Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
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++
#include <bits/stdc++.h>
using namespace std;
int MincntBothPalin(string str1,
string str2, int N)
{
int i = 0;
int j = N - 1;
int cntOp = 0;
while (i < j) {
if (str1[i] == str1[j]
&& str2[i] != str2[j]) {
cntOp += 1;
}
else if (str1[i] != str1[j]
&& str2[i] == str2[j]) {
cntOp += 1;
}
else if (str1[i] != str1[j]
&& str2[i] != str2[j]) {
if (str1[i] == str2[j]
&& str2[i] == str1[j]) {
cntOp += 1;
}
else {
cntOp += 2;
}
}
i += 1;
j -= 1;
}
return cntOp;
}
int main()
{
string str1 = "dbba" ;
string str2 = "abcd" ;
int N = str1.length();
cout << MincntBothPalin(
str1, str2, N);
}
|
Java
import java.util.*;
class GFG{
static int MincntBothPalin( char [] str1,
char [] str2, int N)
{
int i = 0 ;
int j = N - 1 ;
int cntOp = 0 ;
while (i < j)
{
if (str1[i] == str1[j] &&
str2[i] != str2[j])
{
cntOp += 1 ;
}
else if (str1[i] != str1[j] &&
str2[i] == str2[j])
{
cntOp += 1 ;
}
else if (str1[i] != str1[j] &&
str2[i] != str2[j])
{
if (str1[i] == str2[j] &&
str2[i] == str1[j])
{
cntOp += 1 ;
}
else
{
cntOp += 2 ;
}
}
i += 1 ;
j -= 1 ;
}
return cntOp;
}
public static void main(String[] args)
{
String str1 = "dbba" ;
String str2 = "abcd" ;
int N = str1.length();
System.out.print(MincntBothPalin(
str1.toCharArray(), str2.toCharArray(), N));
}
}
|
Python3
def MincntBothPalin(str1,
str2, N):
i = 0
j = N - 1
cntOp = 0
while (i < j):
if (str1[i] = = str1[j] and
str2[i] ! = str2[j]):
cntOp + = 1
elif (str1[i] ! = str1[j] and
str2[i] = = str2[j]):
cntOp + = 1
elif (str1[i] ! = str1[j] and
str2[i] ! = str2[j]):
if (str1[i] = = str2[j] and
str2[i] = = str1[j]):
cntOp + = 1
else :
cntOp + = 2
i + = 1
j - = 1
return cntOp
if __name__ = = "__main__" :
str1 = "dbba"
str2 = "abcd"
N = len (str1)
print (MincntBothPalin(str1,
str2, N))
|
C#
using System;
class GFG{
static int MincntBothPalin( string str1,
string str2, int N)
{
int i = 0;
int j = N - 1;
int cntOp = 0;
while (i < j)
{
if (str1[i] == str1[j] &&
str2[i] != str2[j])
{
cntOp += 1;
}
else if (str1[i] != str1[j] &&
str2[i] == str2[j])
{
cntOp += 1;
}
else if (str1[i] != str1[j] &&
str2[i] != str2[j])
{
if (str1[i] == str2[j] &&
str2[i] == str1[j])
{
cntOp += 1;
}
else
{
cntOp += 2;
}
}
i += 1;
j -= 1;
}
return cntOp;
}
public static void Main()
{
string str1 = "dbba" ;
string str2 = "abcd" ;
int N = str1.Length;
Console.WriteLine(
MincntBothPalin(str1,
str2, N));
}
}
|
Javascript
<script>
function MincntBothPalin(str1, str2, N)
{
var i = 0;
var j = N - 1;
var cntOp = 0;
while (i < j) {
if (str1[i] === str1[j] &&
str2[i] !== str2[j]) {
cntOp += 1;
}
else if (str1[i] !== str1[j] &&
str2[i] === str2[j]) {
cntOp += 1;
}
else if (str1[i] !== str1[j] &&
str2[i] !== str2[j])
{
if (str1[i] === str2[j] &&
str2[i] === str1[j]) {
cntOp += 1;
} else {
cntOp += 2;
}
}
i += 1;
j -= 1;
}
return cntOp;
}
var str1 = "dbba" ;
var str2 = "abcd" ;
var N = str1.length;
document.write(MincntBothPalin(str1, str2, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...