📌  相关文章
📜  最小化相同索引字符的交换,以使两个字符串的字符的ASCII值之和为奇数

📅  最后修改于: 2021-04-17 17:34:39             🧑  作者: Mango

给定两个N -length字符串ST由小写字母的,任务是尽量减少相同索引元素的交换次数需要使奇数两个字符串的字符的ASCII值的总和。如果不可能使ASCII值之和为奇数,则打印“ -1”

例子:

方法:请按照以下步骤解决问题:

  • 计算字符串ST的字符的ASCII值之和,并将其分别存储在变量sum1sum2中
  • 如果sum1sum2已经是奇数,则打印0 ,因为不需要交换。
  • 如果sum1sum2具有不同的奇偶校验,则打印-1 ,因为两个字符串的总和不能具有相同的奇偶校验。
  • 如果sum1sum2均为偶数,则遍历给定的字符串ST。如果存在具有奇数ASCII值的任何字符,这两个字符串的字符的ASCII值的总和可以通过奇数仅1交换制备。否则,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of swaps
// required to make the sum of ASCII values
// of the characters of both strings odd
void countSwaps(string S, string T)
{
    // Initialize alphabets with value
    int value[26];
 
    // Initialize values for each
    // alphabet
    for (int i = 0; i < 26; i++)
        value[i] = i + 1;
 
    // Size of the string
    int N = S.size();
 
    // Sum of string S
    int sum1 = 0;
 
    // Sum of string T
    int sum2 = 0;
 
    // Stores whether there is any
    // index i such that S[i] and
    // T[i] have different parities
    bool flag = false;
 
    // Traverse the strings
    for (int i = 0; i < N; i++) {
 
        // Update sum1 and sum2
        sum1 += value[S[i] - 'a'];
        sum2 += value[T[i] - 'a'];
 
        // If S[i] and T[i] have
        // different parities
        if ((value[S[i] - 'a'] % 2 == 0
             && value[T[i] - 'a'] % 2 == 1)
 
            || (value[S[i] - 'a'] % 2 == 1
                && value[T[i] - 'a'] % 2 == 0))
 
            flag = false;
    }
 
    // If sum1 and sum2 are both odd
    if (sum1 % 2 == 1
        && sum2 % 2 == 1)
        cout << "0\n";
 
    // If sum1 and sum2 are both even
    else if (sum1 % 2 == 0
             && sum2 % 2 == 0) {
 
        // If exists print 1
        if (flag)
            cout << "1";
 
        // Otherwise
        else
            cout << "-1";
    }
 
    // If sum1 and sum2 are
    // of different parities
    else {
        cout << "-1";
    }
}
 
// Driver Code
int main()
{
    string S = "acd";
    string T = "dbf";
 
    // Function Call
    countSwaps(S, T);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to count the number of swaps
  // required to make the sum of ASCII values
  // of the characters of both strings odd
  static void countSwaps(String S, String T)
  {
 
    // Initialize alphabets with value
    int[] value = new int[26];
 
    // Initialize values for each
    // alphabet
    for (int i = 0; i < 26; i++)
      value[i] = i + 1;
 
    // Size of the string
    int N = S.length();
 
    // Sum of string S
    int sum1 = 0;
 
    // Sum of string T
    int sum2 = 0;
 
    // Stores whether there is any
    // index i such that S[i] and
    // T[i] have different parities
    boolean flag = false;
 
    // Traverse the strings
    for (int i = 0; i < N; i++) {
 
      // Update sum1 and sum2
      sum1 += value[S.charAt(i) - 'a'];
      sum2 += value[T.charAt(i) - 'a'];
 
      // If S[i] and T[i] have
      // different parities
      if ((value[S.charAt(i) - 'a'] % 2 == 0
           && value[T.charAt(i) - 'a'] % 2 == 1)
 
          || (value[S.charAt(i) - 'a'] % 2 == 1
              && value[T.charAt(i) - 'a'] % 2 == 0))
 
        flag = false;
    }
 
    // If sum1 and sum2 are both odd
    if (sum1 % 2 == 1
        && sum2 % 2 == 1)
      System.out.println("0\n");
 
    // If sum1 and sum2 are both even
    else if (sum1 % 2 == 0
             && sum2 % 2 == 0) {
 
      // If exists print 1
      if (flag)
        System.out.println("1");
 
      // Otherwise
      else
        System.out.println("-1");
    }
 
    // If sum1 and sum2 are
    // of different parities
    else {
      System.out.println("-1");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    String S = "acd";
    String T = "dbf";
 
    // Function Call
    countSwaps(S, T);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python3 program for the above approach
 
# Function to count the number of swaps
# required to make the sum of ASCII values
# of the characters of both strings odd
def countSwaps(S, T):
   
    # Initialize alphabets with value
    value = [0]*26
 
    # Initialize values for each
    # alphabet
    for i in range(26):
        value[i] = i + 1
 
    # Size of the string
    N = len(S)
 
    # Sum of S
    sum1 = 0
 
    # Sum of T
    sum2 = 0
 
    # Stores whether there is any
    # index i such that S[i] and
    # T[i] have different parities
    flag = False
 
    # Traverse the strings
    for i in range(N):
 
        # Update sum1 and sum2
        sum1 += value[ord(S[i]) - ord('a')]
        sum2 += value[ord(T[i]) - ord('a')]
 
        # If ord(S[i]) anord('a)rd(T[i]) haord('a)
        # different parities
        if (value[ord(S[i]) - ord('a')] % 2 == 0
            and value[ord(T[i]) - ord('a')] % 2 == 1
            or value[ord(S[i]) - ord('a')] % 2 == 1
            and value[ord(T[i]) - ord('a')] % 2 == 0):
             
            flag = False
 
    # If sum1 and sum2 are both odd
    if (sum1 % 2 == 1 and sum2 % 2 == 1):
        print("0")
 
    # If sum1 and sum2 are both even
    elif (sum1 % 2 == 0 and sum2 % 2 == 0):
 
        # If exists pr1
        if (flag):
            print("1")
 
        # Otherwise
        else:
            print("-1")
 
    # If sum1 and sum2 are
    # of different parities
    else:
        print("-1")
 
# Driver Code
if __name__ == '__main__':
    S = "acd"
    T = "dbf"
 
    # Function Call
    countSwaps(S, T)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
public class GFG
{
   
// Function to count the number of swaps
// required to make the sum of ASCII values
// of the characters of both strings odd
static void countSwaps(string S, string T)
{
   
    // Initialize alphabets with value
    int[] value = new int[26];
 
    // Initialize values for each
    // alphabet
    for (int i = 0; i < 26; i++)
        value[i] = i + 1;
 
    // Size of the string
    int N = S.Length;
 
    // Sum of string S
    int sum1 = 0;
 
    // Sum of string T
    int sum2 = 0;
 
    // Stores whether there is any
    // index i such that S[i] and
    // T[i] have different parities
    bool flag = false;
 
    // Traverse the strings
    for (int i = 0; i < N; i++) {
 
        // Update sum1 and sum2
        sum1 += value[S[i] - 'a'];
        sum2 += value[T[i] - 'a'];
 
        // If S[i] and T[i] have
        // different parities
        if ((value[S[i] - 'a'] % 2 == 0
             && value[T[i] - 'a'] % 2 == 1)
 
            || (value[S[i] - 'a'] % 2 == 1
                && value[T[i] - 'a'] % 2 == 0))
 
            flag = false;
    }
 
    // If sum1 and sum2 are both odd
    if (sum1 % 2 == 1
        && sum2 % 2 == 1)
        Console.Write("0\n");
 
    // If sum1 and sum2 are both even
    else if (sum1 % 2 == 0
             && sum2 % 2 == 0) {
 
        // If exists print 1
        if (flag)
            Console.Write("1");
 
        // Otherwise
        else
            Console.Write("-1");
    }
 
    // If sum1 and sum2 are
    // of different parities
    else {
        Console.Write("-1");
    }
}
 
 
// Driver Code
public static void Main(String[] args)
{
    string S = "acd";
    string T = "dbf";
 
    // Function Call
    countSwaps(S, T);
}
}
 
// This code is contributed by code_hunt.


输出:
-1

时间复杂度: O(N)
辅助空间: O(26)