Minimize swaps of same-indexed characters to make sum of ASCII value of characters of both the strings odd


In this article, we delve into a fascinating problem of string manipulation and character encoding in computer science. The task at hand is to minimize the number of swaps between same-indexed characters of two strings to make the sum of ASCII values of characters in both strings odd. A robust and versatile programming language favored by many software developers.

Understanding ASCII

ASCII, short for American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices that use text.

Problem Statement

We are given two strings of equal length. The goal is to perform minimum swaps of characters at the same positions in both strings so that the sum of the ASCII values of characters in each string is odd.

Approach to Solution

  • Calculate ASCII Sum  Compute the sum of ASCII values for each string. Then, check if the sum is even or odd.

  • Determine Swap Requirement  If the sum is already odd, no swaps are needed. If the sum is even, swaps are required.

  • Find Eligible Swap  Look for characters in both strings where a swap would result in an odd sum. Track the number of swaps.

  • Return Result  Return the minimum number of swaps required.

Example

Here's the revised codes that cater to all scenarios −

#include <stdio.h>
#include <string.h>

int minSwaps(char* str1, char* str2) {
   int len = strlen(str1);
   int ascii_sum1 = 0, ascii_sum2 = 0;

   for (int i = 0; i < len; i++) {
      ascii_sum1 += str1[i];
      ascii_sum2 += str2[i];
   }

   // If total sum is odd, it's impossible to have both sums odd
   if ((ascii_sum1 + ascii_sum2) % 2 != 0) return -1;

   // If both sums are odd already, no swaps are needed
   if (ascii_sum1 % 2 != 0 && ascii_sum2 % 2 != 0) return 0;

   // If both sums are even, we just need to make one of them odd
   if (ascii_sum1 % 2 == 0 && ascii_sum2 % 2 == 0) {
      for (int i = 0; i < len; i++) {
         if ((str1[i] - '0') % 2 != (str2[i] - '0') % 2) return 1;
      }
   }

   // If we reach here, it means no eligible swaps were found
   return -1;
}
int main() {
   char str1[] = "abc";
   char str2[] = "def";

   int result = minSwaps(str1, str2);
   if (result == -1) {
      printf("No valid swaps found.\n");
   } else {
      printf("Minimum swaps required: %d\n", result);
   }

   return 0;
}

Output

No valid swaps found.
#include <bits/stdc++.h>
using namespace std;

int minSwaps(string str1, string str2) {
   int len = str1.length();
   int ascii_sum1 = 0, ascii_sum2 = 0;
   
   for (int i = 0; i < len; i++) {
      ascii_sum1 += str1[i];
      ascii_sum2 += str2[i];
   }
   
   // If total sum is odd, it's impossible to have both sums odd
   if ((ascii_sum1 + ascii_sum2) % 2 != 0) return -1;
   
   // If both sums are odd already, no swaps are needed
   if (ascii_sum1 % 2 != 0 && ascii_sum2 % 2 != 0) return 0;
   
   // If both sums are even, we just need to make one of them odd
   if (ascii_sum1 % 2 == 0 && ascii_sum2 % 2 == 0) {
      for (int i = 0; i < len; i++) {
         if ((str1[i] - '0') % 2 != (str2[i] - '0') % 2) return 1;
      }
   }

   // If we reach here, it means no eligible swaps were found
   return -1;
}
int main() {
   string str1 = "abc";
   string str2 = "def";
   
   int result = minSwaps(str1, str2);
   if(result == -1) {
      cout << "No valid swaps found.\n";
   } else {
      cout << "Minimum swaps required: " << result << endl;
   }
   
   return 0;
}

Output

No valid swaps found.
public class Main {
   public static int minSwaps(String str1, String str2) {
      int len = str1.length();
      int asciiSum1 = 0, asciiSum2 = 0;

      for (int i = 0; i < len; i++) {
         asciiSum1 += str1.charAt(i);
         asciiSum2 += str2.charAt(i);
      }

      // If total sum is odd, it's impossible to have both sums odd
      if ((asciiSum1 + asciiSum2) % 2 != 0)
         return -1;

      // If both sums are odd already, no swaps are needed
      if (asciiSum1 % 2 != 0 && asciiSum2 % 2 != 0)
         return 0;

      // If both sums are even, we just need to make one of them odd
      if (asciiSum1 % 2 == 0 && asciiSum2 % 2 == 0) {
         for (int i = 0; i < len; i++) {
            if ((str1.charAt(i) - '0') % 2 != (str2.charAt(i) - '0') % 2)
               return 1;
         }
      }

      // If we reach here, it means no eligible swaps were found
      return -1;
   }

   public static void main(String[] args) {
      String str1 = "abc";
      String str2 = "def";

      int result = minSwaps(str1, str2);
      if (result == -1) {
         System.out.println("No valid swaps found.");
      } else {
         System.out.println("Minimum swaps required: " + result);
      }
   }
}

Output

No valid swaps found.
def min_swaps(str1, str2):
   length = len(str1)
   ascii_sum1 = sum(ord(ch) for ch in str1)
   ascii_sum2 = sum(ord(ch) for ch in str2)

   # If total sum is odd, it's impossible to have both sums odd
   if (ascii_sum1 + ascii_sum2) % 2 != 0:
      return -1

   # If both sums are odd already, no swaps are needed
   if ascii_sum1 % 2 != 0 and ascii_sum2 % 2 != 0:
      return 0

   # If both sums are even, we just need to make one of them odd
   if ascii_sum1 % 2 == 0 and ascii_sum2 % 2 == 0:
      for i in range(length):
         if int(str1[i]) % 2 != int(str2[i]) % 2:
            return 1

   # If we reach here, it means no eligible swaps were found
   return -1

str1 = "abc"
str2 = "def"

result = min_swaps(str1, str2)
if result == -1:
   print("No valid swaps found.")
else:
   print("Minimum swaps required:", result)

Output

No valid swaps found.

Explanation

Consider two strings −

str1 = "abc", str2 = "def"

We calculate the ASCII sum of str1 (294: a = 97, b = 98, c = 99) and str2 (303: d = 100, e = 101, f = 102). The total ASCII sum is 597, which is odd. Therefore, it's impossible to have both sums odd, and the program will output "No valid swaps found."

This solution efficiently solves the problem using simple programming constructs and logical reasoning.

Conclusion

The task of minimizing swaps to achieve an odd sum of ASCII values is an intriguing problem that enhances our understanding of string manipulation, character encoding, and problem-solving skills. The solution provided uses the various programming languages and demonstrates how to handle different scenarios in a problem statement.

One thing to note is that this solution assumes that both strings have the same length. If they do not, additional logic would be necessary to handle that case.

Updated on: 27-Oct-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements