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++
#include <iostream>
using namespace std;
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;
}
int main()
{
string S1 = "2643";
int N = S1.size();
int A[N];
for (int i = 0; i < N; i++) {
A[i] = S1[i] - '0';
}
minMoves(A, N);
string S2 = "113678";
N = S2.size();
int B[N];
for (int i = 0; i < N; i++) {
B[i] = S2[i] - '0';
}
minMoves(B, N);
}
Java
import java.io.*;
import java.util.*;
public class GFG {
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);
}
public static void main(String[] args)
{
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';
}
minMoves(A, N);
String S2 = "113678";
N = S2.length();
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = S2.charAt(i) - '0';
}
minMoves(B, N);
}
}
Python3
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)
S1 = "2643"
N = len(S1)
A = [0]*N
for i in range(N):
A[i] = int(S1[i]) - 0
minMoves(A, N)
S2 = "113678"
N = len(S2)
B = [0]*N
for i in range(N):
B[i] = int(S2[i]) - 0
minMoves(B, N)
C#
using System;
class GFG {
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);
}
public static void Main()
{
string S1 = "2643";
int N = S1.Length;
int[] A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = S1[i] - '0';
}
minMoves(A, N);
string S2 = "113678";
N = S2.Length;
int[] B = new int[N];
for (int i = 0; i < N; i++) {
B[i] = S2[i] - '0';
}
minMoves(B, N);
}
}
Javascript
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>");
}
let S1 = "2643";
let N = S1.length;
let A=new Array(N);;
for (let i = 0; i < N; i++) {
A[i] = S1[i] - '0';
}
minMoves(A, N);
let S2 = "113678";
N = S2.length;
let B=new Array(N);;
for (let i = 0; i < N; i++) {
B[i] = S2[i] - '0';
}
minMoves(B, N);
Time Complexity: O(N)Auxiliary Space: O(N)
Related Articles:
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...