Minimize cost to convert given string to a palindrome
Given a string S of length N and an integer P denoting a pointer to Pth index of the string, the task is to find the minimum cost to convert the string into a palindrome by performing the following operations:
- Pointer P can be moved from index i to index j and the cost required is |i – j| where 0 ? i < N and 0 ? j < N.
- The cost to change the character at the Pth index is 1.
Examples:
Input: S = “saad”, P = 1
Output: 2
Explanation:
Initially the pointer is at index 1.
Step 1: Move pointer to index 0. Therefore, cost = 1 – 0 = 1.
Step 2: Change character s to d. Therefore, cost = 1 + 1 = 2 and S = “daad”
Hence, the cost is 2.
Input: S = “bass”, P = 3
Output: 3
Explanation:
Initially the pointer is at index 3.
Step 1: Change character at index P = 3 to b. Therefore, cost = 1 and S = “basb”.
Step 2: Move pointer to index 2. Therefore, cost = 1 + 1 = 2.
Step 3: Change character at index P = 2 to a. Therefore, cost = 3 and S = “baab”
Hence, the cost is 3.
Approach: The idea is to find the first and the last character that needs to be changed in the first half of the string when the pointer is in the first half or reverse the string if the pointer is in the second half and adjust the pointer accordingly. Follow the below steps to solve the problem:
- If the pointer is in the second half then reverse the string and change the pointer to (N – 1 – P).
- Now, find the farthest character’s position needed to change at the left and right side in the first half of the string. Let them be l and r.
- Find the total cost to change the characters i.e., total characters in the first half such that S[i] != s[N – i – 1] where 0 < i < N/2.
- The minimum distance traverse to change the characters is min(2*(P – l) + r – P, 2*(r – P) + P – l).
- Print the answer as the sum of the cost to change the characters and minimum distance to travel.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinCost(string str, int pos)
{
int n = str.length();
if (pos >= n / 2) {
reverse(str.begin(), str.end());
pos = n - pos - 1;
}
int left, right;
left = right = pos;
for ( int i = pos; i >= 0; --i) {
if (str[i] != str[n - i - 1]) {
left = i;
}
}
for ( int i = pos; i < n / 2; ++i) {
if (str[i] != str[n - i - 1]) {
right = i;
}
}
int ans = 0;
for ( int i = left; i <= right; ++i) {
if (str[i] != str[n - i - 1])
ans += 1;
}
int dis = min((2 * (pos - left)
+ (right - pos)),
(2 * (right - pos)
+ (pos - left)));
ans = ans + dis;
return ans;
}
int main()
{
string S = "bass" ;
int P = 3;
cout << findMinCost(S, P);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMinCost(String str, int pos)
{
int n = str.length();
StringBuilder input1 = new StringBuilder();
input1.append(str);
if (pos >= n / 2 )
{
input1 = input1.reverse();
pos = n - pos - 1 ;
}
int left, right;
left = right = pos;
for ( int i = pos; i >= 0 ; --i)
{
if (input1.charAt(i) !=
input1.charAt(n - i - 1 ))
{
left = i;
}
}
for ( int i = pos; i < n / 2 ; ++i)
{
if (input1.charAt(i) !=
input1.charAt(n - i - 1 ))
{
right = i;
}
}
int ans = 0 ;
for ( int i = left; i <= right; ++i)
{
if (input1.charAt(i) !=
input1.charAt(n - i - 1 ))
ans += 1 ;
}
int dis = Math.min(( 2 * (pos - left) +
(right - pos)),
( 2 * (right - pos) +
(pos - left)));
ans = ans + dis;
return ans;
}
public static void main(String args[])
{
String S = "bass" ;
int P = 3 ;
System.out.println(findMinCost(S, P));
}
}
|
Python3
def findMinCost(strr, pos):
n = len (strr)
if (pos > = n / 2 ):
strr = strr[:: - 1 ]
pos = n - pos - 1
left, right = pos, pos
for i in range (pos, - 1 , - 1 ):
if (strr[i] ! = strr[n - i - 1 ]):
left = i
for i in range (pos, n / / 2 ):
if (strr[i] ! = strr[n - i - 1 ]):
right = i
ans = 0
for i in range (left, right + 1 ):
if (strr[i] ! = strr[n - i - 1 ]):
ans + = 1
dis = ( min (( 2 * (pos - left) +
(right - pos)),
( 2 * (right - pos) +
(pos - left))))
ans = ans + dis
return ans
if __name__ = = '__main__' :
S = "bass"
P = 3
print (findMinCost(S, P))
|
C#
using System;
class GFG{
static int findMinCost( string str,
int pos)
{
int n = str.Length;
char [] charArray =
str.ToCharArray();
if (pos >= n / 2)
{
Array.Reverse(charArray);
pos = n - pos - 1;
}
int left, right;
left = right = pos;
for ( int i = pos; i >= 0; --i)
{
if (charArray[i] !=
charArray[n - i - 1])
{
left = i;
}
}
for ( int i = pos; i < n / 2; ++i)
{
if (charArray[i] !=
charArray[n - i - 1])
{
right = i;
}
}
int ans = 0;
for ( int i = left; i <= right; ++i)
{
if (charArray[i]!=
charArray[n - i - 1])
ans += 1;
}
int dis = Math.Min((2 * (pos - left) +
(right - pos)),
(2 * (right - pos) +
(pos - left)));
ans = ans + dis;
return ans;
}
public static void Main()
{
string S = "bass" ;
int P = 3;
Console.Write(findMinCost(S, P));
}
}
|
Javascript
<script>
function findMinCost(str, pos)
{
var n = str.length;
if (pos >= n / 2)
{
str.split( '' ).reverse().fill( '' );
pos = n - pos - 1;
}
var left, right;
left = right = pos;
for ( var i = pos; i >= 0; --i)
{
if (str[i] != str[n - i - 1])
{
left = i;
}
}
for ( var i = pos; i < n / 2; ++i)
{
if (str[i] != str[n - i - 1])
{
right = i;
}
}
var ans = 0;
for ( var i = left; i <= right; ++i)
{
if (str[i] != str[n - i - 1])
ans += 1;
}
var dis = Math.min((2 * (pos - left) +
(right - pos)),
(2 * (right - pos) +
(pos - left)));
ans = ans + dis;
return ans;
}
var S = "bass" ;
var P = 3;
document.write(findMinCost(S, P));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
26 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...