Given two strings, str1 and str2 consisting of N lowercase alphabets, the task is to find the minimum count of operations of the following two types to make both the strings palindromic string.
- Replace any character of the strings to any other character([a – z]).
- Swap any two characters present at the same index in both the strings.
Examples:
Input: str1 = “abbd”, str2 = “dbca”
Output: 2
Explanation:
Swapping (str1[0], str2[0]) modifies strings str1 to “dbbd” and str2 to “abca”
Replacing str2[1] to ‘c’ modifies string str2 to “acca”.
Therefore, after above 2 operations, strings str1 and str2 become palindromic.Input: str1 = “geeksforgeeks”, str2 = “geeksforgeeks”
Output: 10
Approach: Follow the steps below to solve the problem:
- Initialize two variables, say i = 0 and j = 0 to store the index of left pointer and right pointer of both the strings respectively.
- Initialize a variable, say cntOp to store the count of minimum operations required to make both the strings palindromic string.
- Traverse both the string and check the following conditions:
- If str1[i] == str1[j] and str2[i] != str2[j] then replace the value of str2[i] to str2[j] and increment the value of cntOp by 1.
- If str1[i] != str1[j] and str2[i] == str2[j] then replace the value of str1[i] to str1[j] and increment the value of cntOp by 1.
- If str1[i] != str1[j] and str2[i] != str2[j] then check the if the value of (str1[i] == str2[j] and str2[i] == str1[j]) equal to true or not. If found to be true then swap(str1[i], str2[j]) and increment the value of cntOp by 1.
- Otherwise, replace str1[i] to str1[j], str2[i] to str2[j] and increment the value of cntOp by 2.
- Finally, print the value of cntOp.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum operations // to make both the strings palindromic int MincntBothPalin(string str1, string str2, int N) { // Stores index of // the left pointer int i = 0; // Stores index of // the right pointer int j = N - 1; // Stores count of minimum operations to // make both the strings palindromic int cntOp = 0; while (i < j) { // if str1[i] equal to str1[j] // and str2[i] not equal to str2[j] if (str1[i] == str1[j] && str2[i] != str2[j]) { // Update cntOp cntOp += 1; } // If str1[i] not equal to str1[j] // and str2[i] equal to str2[j] else if (str1[i] != str1[j] && str2[i] == str2[j]) { // Update cntOp cntOp += 1; } // If str1[i] is not equal to str1[j] // and str2[i] is not equal to str2[j] else if (str1[i] != str1[j] && str2[i] != str2[j]) { // If str1[i] is equal to str2[j] // and str2[i] is equal to str1[j] if (str1[i] == str2[j] && str2[i] == str1[j]) { // Update cntOp cntOp += 1; } else { // Update cntOp cntOp += 2; } } // Update i and j i += 1; j -= 1; } return cntOp; } // Driver Code int main() { string str1 = "dbba"; string str2 = "abcd"; // Stores length of str1 int N = str1.length(); cout << MincntBothPalin( str1, str2, N); } |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimize count of given operations required to make two given strings permutations of each other
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements in a string to make adjacent characters unequal
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum number of pairs required to make two strings same
- Minimum swaps to make two strings equal by swapping only with third string
- Minimize characters to be changed to make the left and right rotation of a string same
- Check whether two strings contain same characters in same order
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

