- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 a code implementation in C++. 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 is the C++ code implementation for the above approach −
#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
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.