Open In App
Related Articles

Minimum moves to make String Palindrome incrementing all characters of Substrings

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string S of length N, the task is to find the minimum number of moves required to make a string palindrome where in each move you can select any substring and increment all the characters of the substring by 1.

Examples:

Input: S = “264341”
Output: 2
?Explanation: We can perform the following operations:
Select the substring  “4341”. The string will be  “265452” 
Select the substring “45”. The string will be “265562”.
Hence after performing 2 operation on string S we get palindrome.

Input: S = “121”
Output: 0

Approach: To solve the problem follow the below approach:

We want to make S a palindrome, so let’s look at corresponding pairs in S  i.e, pairs of indices (i, N-1?i). If S[i] ? S[N-i-1] then S[i] should be incremented to S[N-i-1]. For each index find out the minimum number moves required to make that character same to its counterpart in the palindrome. 

Now optimally choose a substring where minimum number of operations should be performed on that substring. This can be done greedily by checking for substring where the requirement for (i+1)th index is at least the same as ith index.

Follow the steps mentioned below to implement the idea: 

  • Create a new array B[] of length N to find the number of moves required for each character. So where B[i] = S[N-i-1] – S[i], if S[i] is at most S[N-i-1].Otherwise, B[i] = 0.
  • Iterate from i = 0 to N:
    • If i = 0, perform, perform B[0] operations on the first index.
    • For other indices:
      • If B[i] ? B[i-1], we can extend B[i]  of the operations performed on index (i-1) to also cover this index, for no extra cost.
      • If B[i-1] < B[i] extra operations should start at starting at index i.
      • The above two cases can be combined to say that we perform max(0, B[i] ? B[i-1]) operations starting at index 1.
  • So, the final answer is simply [N i=2? max(0, B[i] ? B[i?1])] 

Below is the implementation of the above approach.

C++

// C++ code to implement the approach
#include <iostream>
using namespace std;
 
// Function to Find the minimum number of moves
// to make string palindrome
void minMoves(int a[], int n)
{
  int b[n]={0};
  for (int i = 0; i < n; i++) {
    if (a[i] > a[n - i - 1]) {
      b[i] = a[i] - a[n - i - 1];
    }
  }
  long ans = b[0];
  for (int i = 1; i < n; i++) {
    ans += max(0, b[i] - b[i - 1]);
  }
  cout<<(ans)<<endl;
}
 
// Driver Code
int main()
{
  // Testcase1
  string S1 = "2643";
  int N = S1.size();
  int A[N];
  for (int i = 0; i < N; i++) {
    A[i] = S1[i] - '0';
  }
 
  // Function Call
  minMoves(A, N);
 
  // Testcase2
  string S2 = "113678";
  N = S2.size();
  int B[N];
  for (int i = 0; i < N; i++) {
    B[i] = S2[i] - '0';
  }
 
  // Function Call
  minMoves(B, N);
}
 
 
// This code is contributed by Aman Kumar.

                    

Java

// Java code to implement the approach
 
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to Find the minimum number of moves
    // to make string palindrome
    public static void minMoves(int a[], int n)
    {
        int b[] = new int[n];
        for (int i = 0; i < n; i++) {
            if (a[i] > a[n - i - 1]) {
                b[i] = a[i] - a[n - i - 1];
            }
        }
        long ans = b[0];
        for (int i = 1; i < n; i++) {
            ans += Math.max(0, b[i] - b[i - 1]);
        }
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Testcase1
        String S1 = "2643";
        int N = S1.length();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = S1.charAt(i) - '0';
        }
 
        // Function Call
        minMoves(A, N);
 
        // Testcase2
        String S2 = "113678";
        N = S2.length();
        int[] B = new int[N];
        for (int i = 0; i < N; i++) {
            B[i] = S2.charAt(i) - '0';
        }
 
        // Function Call
        minMoves(B, N);
    }
}

                    

Python3

# Python code to implement the approach
 
# Function to find the minimum number
# of moves to make string palindrome
def minMoves(a, n):
    b = [0]*n
    for i in range(n):
        if(a[i] > a[n-i-1]):
            b[i] = a[i] - a[n-i-1]
 
    ans = b[0]
    for i in range(1, n):
        ans = ans + max(0, b[i] - b[i-1])
    print(ans)
 
# Test case 1
S1 = "2643"
N = len(S1)
A = [0]*N
for i in range(N):
    A[i] = int(S1[i]) - 0
 
# Function call
minMoves(A, N)
 
# Test case 2
S2 = "113678"
N = len(S2)
B = [0]*N
for i in range(N):
    B[i] = int(S2[i]) - 0
 
# Function call
minMoves(B, N)
 
# This code is contributed by lokesh.

                    

C#

// C# code to implement the approach
using System;
 
class GFG {
 
    // Function to Find the minimum number of moves
    // to make string palindrome
    static void minMoves(int[] a, int n)
    {
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            if (a[i] > a[n - i - 1]) {
                b[i] = a[i] - a[n - i - 1];
            }
        }
        long ans = b[0];
        for (int i = 1; i < n; i++) {
            ans += Math.Max(0, b[i] - b[i - 1]);
        }
        Console.WriteLine(ans);
    }
 
    // Driver Code
    public static void Main()
    {
        // Testcase1
        string S1 = "2643";
        int N = S1.Length;
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = S1[i] - '0';
        }
 
        // Function Call
        minMoves(A, N);
 
        // Testcase2
        string S2 = "113678";
        N = S2.Length;
        int[] B = new int[N];
        for (int i = 0; i < N; i++) {
            B[i] = S2[i] - '0';
        }
 
        // Function Call
        minMoves(B, N);
    }
}
 
// This code is contributed by Samim Hossain Mondal.

                    

Javascript

// Javascript code to implement the approach
 
// Function to Find the minimum number of moves
// to make string palindrome
function minMoves(a, n)
{
let b=Array(n).fill(0);
for (let i = 0; i < n; i++) {
    if (a[i] > a[n - i - 1]) {
    b[i] = a[i] - a[n - i - 1];
    }
}
let ans = b[0];
for (let i = 1; i < n; i++) {
    ans += Math.max(0, b[i] - b[i - 1]);
}
console.log(ans+"<br>");
}
 
// Driver Code
 
// Testcase1
let S1 = "2643";
let N = S1.length;
let A=new Array(N);;
for (let i = 0; i < N; i++) {
    A[i] = S1[i] - '0';
}
 
// Function Call
minMoves(A, N);
 
// Testcase2
let S2 = "113678";
N = S2.length;
let B=new Array(N);;
for (let i = 0; i < N; i++) {
    B[i] = S2[i] - '0';
}
 
// Function Call
minMoves(B, N);
 
// This code is contributed by Pushpesh Raj

                    

Output
3
7

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

Related Articles:



Last Updated : 21 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads