Open In App

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.

Examples:



Input: str1 = “abbd”, str2 = “dbca” 
Output:
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:

Below is the implementation of the above approach:




// 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);
}




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find minimum operations
// to make both the Strings palindromic
static int MincntBothPalin(char[] str1,
                           char[] 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
public static void main(String[] args)
{
    String str1 = "dbba";
    String str2 = "abcd";
 
    // Stores length of str1
    int N = str1.length();
     
    System.out.print(MincntBothPalin(
        str1.toCharArray(), str2.toCharArray(), N));
}
}
 
// This code is contributed by Amit Katiyar




# Python3 program to implement
# the above approach
 
# Function to find minimum
# operations to make both
# the strings palindromic
def MincntBothPalin(str1,
                    str2, N):
 
    # Stores index of
    # the left pointer
    i = 0
 
    # Stores index of
    # the right pointer
    j = N - 1
 
    # Stores count of minimum
    # operations to make both
    # the strings palindromic
    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] and
            str2[i] != str2[j]):
 
            # Update cntOp
            cntOp += 1
 
        # If str1[i] not equal
        # to str1[j] and str2[i]
        # equal to str2[j]
        elif (str1[i] != str1[j] and
              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]
        elif (str1[i] != str1[j] and
              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] and
                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
if __name__ == "__main__":
   
    str1 = "dbba"
    str2 = "abcd"
 
    # Stores length of str1
    N = len(str1)
    print(MincntBothPalin(str1,
                          str2, N))
 
# This code is contributed by Chitranayal




// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to find minimum operations
// to make both the strings palindromic
static 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
public static void Main()
{
  string str1 = "dbba";
  string str2 = "abcd";
 
  // Stores length of str1
  int N = str1.Length;
 
  Console.WriteLine(
  MincntBothPalin(str1,
                  str2, N));
}
}
 
// This code is contributed by bgangwar59




<script>
 
      // JavaScript program to implement
      // the above approach
 
      // Function to find minimum operations
      // to make both the strings palindromic
      function MincntBothPalin(str1, str2, N)
      {
        // Stores index of
        // the left pointer
        var i = 0;
 
        // Stores index of
        // the right pointer
        var j = N - 1;
 
        // Stores count of minimum operations to
        // make both the strings palindromic
        var 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
      var str1 = "dbba";
      var str2 = "abcd";
 
      // Stores length of str1
      var N = str1.length;
      document.write(MincntBothPalin(str1, str2, N));
       
    </script>

Output: 
2

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :