Minimize count of given operations required to make two given strings permutations of each other


In this article, we will discuss how to minimize the count of given operations required to make two given strings permutations of each other. We will follow a step-by-step approach and provide the codes implementation. We will also include an example test case to help understand the problem and the solution.

Problem Statement

Given two strings s1 and s2, we need to find the minimum number of operations required to make s1 and s2 permutations of each other. We can perform two operations: swap any two characters of s1, or swap any two characters of s2.

Approach and Implementation

To solve this problem, we need to count the number of characters that are not present in both strings, i.e., the difference in the frequency of characters in both strings. The minimum number of swaps required to make both strings permutations of each other is equal to half of this count, as we can swap the characters in either of the strings to make them equal.

Firstly, we will count the frequency of characters in both strings using two arrays. Then, we will iterate through both arrays and add the absolute difference between the frequency of characters to a variable. This variable will store the count of characters that are not present in both strings.

After calculating the count, we will return half of it as the minimum number of swaps required to make both strings permutations of each other.

Example

Below are the following programs for the above approach −

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

int countMinSwaps(char* s1, char* s2) {
   int freq1[26] = {0}, freq2[26] = {0}, count = 0;
   
   // Calculate the frequency of characters in the first string (s1)
   for (int i = 0; s1[i] != '\0'; i++) {
      freq1[s1[i] - 'a']++;
   }
   
   // Calculate the frequency of characters in the second string (s2)
   for (int i = 0; s2[i] != '\0'; i++) {
      freq2[s2[i] - 'a']++;
   }
   
   // Calculate the absolute difference in character frequencies between s1 and s2
   for (int i = 0; i < 26; i++) {
      count += abs(freq1[i] - freq2[i]);
   }
   
   return count / 2; // Divide by 2 since we are counting swaps (one swap will fix two characters)
}

int main() {
   char s1[] = "hello";
   char s2[] = "world";
   int minSwaps = countMinSwaps(s1, s2);
   printf("Minimum number of swaps required: %d\n", minSwaps);
   return 0;
}

Output

Minimum number of swaps required: 3
#include<bits/stdc++.h>
using namespace std;

int countMinSwaps(string s1, string s2) {
   int freq1[26] = {0}, freq2[26] = {0}, count = 0;
   for (char c : s1) {
      freq1[c - 'a']++;
   }
   for (char c : s2) {
      freq2[c - 'a']++;
   }
   for (int i = 0; i < 26; i++) {
      count += abs(freq1[i] - freq2[i]);
   }
   return count / 2;
}

int main() {
   string s1 = "hello";
   string s2 = "world";
   int minSwaps = countMinSwaps(s1, s2);
   cout << "Minimum number of swaps required: " << minSwaps << endl;
   return 0;
}

Output

Minimum number of swaps required: 3
import java.util.Scanner;

public class Main {
   public static int countMinSwaps(String s1, String s2) {
      int[] freq1 = new int[26];
      int[] freq2 = new int[26];
      int count = 0;

      // Calculate the frequency of characters in the first string (s1)
      for (char c : s1.toCharArray()) {
         freq1[c - 'a']++;
      }

      // Calculate the frequency of characters in the second string (s2)
      for (char c : s2.toCharArray()) {
         freq2[c - 'a']++;
      }

      // Calculate the absolute difference in character frequencies between s1 and s2
      for (int i = 0; i < 26; i++) {
         count += Math.abs(freq1[i] - freq2[i]);
      }

      return count / 2; // Divide by 2 since we are counting swaps (one swap will fix two characters)
   }

   public static void main(String[] args) {
      String s1 = "hello";
      String s2 = "world";
      int minSwaps = countMinSwaps(s1, s2);
      System.out.println("Minimum number of swaps required: " + minSwaps);
   }
}

Output

Minimum number of swaps required: 3
def count_min_swaps(s1, s2):
   freq1 = [0] * 26
   freq2 = [0] * 26
   count = 0

   # Calculate the frequency of characters in the first string (s1)
   for c in s1:
      freq1[ord(c) - ord('a')] += 1

   # Calculate the frequency of characters in the second string (s2)
   for c in s2:
      freq2[ord(c) - ord('a')] += 1

   # Calculate the absolute difference in character frequencies between s1 and s2
   for i in range(26):
      count += abs(freq1[i] - freq2[i])

   return count // 2  # Divide by 2 since we are counting swaps (one swap will fix two characters)

def main():
   s1 = "hello"
   s2 = "world"
   min_swaps = count_min_swaps(s1, s2)
   print("Minimum number of swaps required:", min_swaps)

if __name__ == "__main__":
   main()

Output

Minimum number of swaps required: 3

Example Test Case

Let's consider the example strings "hello" and "world" for this test case.

The frequency arrays for both strings will be as follows −

freq1 = {0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
freq2 = {0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}

We can see that the character 'l' has a frequency of 2 in s1 but only 1 in s2, while the character 'r' has a frequency of 1 in s2 but is not present in s1. Therefore, the count of characters that are not present in both strings is 3.

Hence, the minimum number of swaps required to make both strings permutations of each other is 1. We can swap the 'l' in s1 with the 'r' in s2 to obtain the strings "herlo" and "wolld", which are permutations of each other.

Conclusion

In this article, we discussed how to minimize the count of given operations required to make two given strings permutations of each other. We followed a step-by-step approach and provided a code implementation in C++. We also included an example test case to help understand the problem and the solution. This problem can be solved in O(n) time complexity and O(1) space complexity.

Updated on: 23-Oct-2023

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements