Home > Backend Development > C++ > Minimize the number of operations required such that two given strings are permutations of each other

Minimize the number of operations required such that two given strings are permutations of each other

WBOY
Release: 2023-09-17 18:05:02
forward
587 people have browsed it

Minimize the number of operations required such that two given strings are permutations of each other

In this article, we will discuss how to minimize the number of given operations required to align two given strings with each other. We will follow the step-by-step approach and provide C code implementation. We will also provide a sample test case to help understand the problem and solution.

Problem Statement

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

Method and Implementation

In order to solve this problem, we need to count the number of characters that do not exist in the two strings, that is, the difference in the frequency of occurrence of characters in the two strings. The minimum number of swaps required to make two strings permute each other is equal to half this count, since we can swap characters in either string to make them equal.

First, we will use two arrays to calculate the frequency of characters in two strings. We will then iterate through the two arrays and add the absolute difference between the character frequencies to a variable. This variable will store the number of characters that are not present in both strings.

After calculating the count, we return half of it as the minimum number of swaps required for the two strings to be permuted with each other.

Example

The following is the C code implementation of the above method -

#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;
}
Copy after login

Output

Minimum number of swaps required: 3
Copy after login

Example test case

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

The frequency arrays of the two strings are 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}
Copy after login

We can see that the character "l" appears with frequency 2 in s1 but only 1 in s2, while the character "r" appears with frequency 1 in s2 but does not exist in s1. Therefore, the number of characters that are not present in both strings is 3.

Therefore, the minimum number of exchanges required for two strings to be arranged with each other is 1. We can swap the "l" in s1 with the "r" in s2 to get the strings "herlo" and "wolld", which are permutations of each other.

in conclusion

In this article, we discussed how to minimize the number of given operations required to align two given strings with each other. We followed a step-by-step approach and provided C code implementation. We also provide a sample test case to help understand the problem and solution. The problem can be solved in O(n) time complexity and O(1) space complexity.

The above is the detailed content of Minimize the number of operations required such that two given strings are permutations of each other. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template