Minimize replacement of characters to its nearest alphabet to make a string palindromic
Last Updated :
03 Apr, 2023
Given a string S of length N consisting of lowercase alphabets, the task is to find the minimum number of operations to convert the given string into a palindrome. In one operation, choose any character and replace it by its next or previous alphabet.
Note: The alphabets are cyclic i.e., if z is incremented then it becomes a and if a is decremented then it becomes z.
Examples:
Input: S = “arcehesmz”
Output: 16
Explanation:
The possible transformation that given the minimum count of operation is:
- Decrease 1st character ‘a’ by 1 to get ‘z’. Count of operation = 1
- Decrease 3rd character ‘c’ by 10 to get ‘s’. Count of operations = 1 + 10 = 11
- Increase 8th character ‘m’ by 5 to get ‘r’. Count of operations = 11 + 5 = 16.
Therefore, the total count of operations is 16.
Input: S = “abccdb”
Output: 3
Explanation:
The possible transformation that given the minimum count of operation is:
- Increase 1st character ‘a’ by 1 to get ‘b’. Count of operation = 1
- Increase 2nd character ‘b’ by 2 to get ‘d’. Count of operations = 1 + 2 = 3.
Naive Approach: The simplest approach is to generate all possible strings of length N. Then check for each string, if its a palindrome. If any string is found to be palindromic then find the cost of operation required to convert the given string into that string. Repeat the above steps for all the generated strings and print the minimum cost calculated among all the costs.
Time Complexity: O(26N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to traverse the given string and find the change for each position independently. Follow the below steps to solve the problem:
- Traverse through the given string over the range [0, (N/2) – 1].
- For each character at index i, find the absolute difference between the characters at indices i and (N – i – 1).
- There can be two possible differences i.e., the difference when the character at i is incremented to character at index (N – i – 1) and when it is decremented at that index.
- Take the minimum of both and add it to the result.
- After the above steps, print the minimum cost calculated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(string s)
{
int len = s.length();
int result = 0;
for ( int i = 0; i < len / 2; i++) {
int D1 = max(s[i], s[len - 1 - i])
- min(s[i], s[len - 1 - i]);
int D2 = 26 - D1;
result += min(D1, D2);
}
return result;
}
int main()
{
string s = "abccdb" ;
cout << minOperations(s);
return 0;
}
|
Java
public class GFG{
public static int minOperations(String s)
{
int len = s.length();
int result = 0 ;
for ( int i = 0 ; i < len / 2 ; i++)
{
int D1 = Math.max(s.charAt(i),
s.charAt(len - 1 - i)) -
Math.min(s.charAt(i),
s.charAt(len - 1 - i));
int D2 = 26 - D1;
result += Math.min(D1, D2);
}
return result;
}
public static void main(String[] args)
{
String s = "abccdb" ;
System.out.println(minOperations(s));
}
}
|
Python3
def minOperations(s):
length = len (s)
result = 0
for i in range (length / / 2 ):
D1 = ( ord ( max (s[i], s[length - 1 - i])) -
ord ( min (s[i], s[length - 1 - i])))
D2 = 26 - D1
result + = min (D1, D2)
return result
s = "abccdb"
print (minOperations(s))
|
C#
using System;
class GFG{
public static int minOperations(String s)
{
int len = s.Length;
int result = 0;
for ( int i = 0; i < len / 2; i++)
{
int D1 = Math.Max(s[i],
s[len - 1 - i]) -
Math.Min(s[i],
s[len - 1 - i]);
int D2 = 26 - D1;
result += Math.Min(D1, D2);
}
return result;
}
public static void Main(String[] args)
{
String s = "abccdb" ;
Console.WriteLine(minOperations(s));
}
}
|
Javascript
<script>
function minOperations(s)
{
var len = s.length;
var result = 0;
for ( var i = 0; i < len / 2; i++)
{
var D1 =
Math.max(s[i].charCodeAt(0), s[len - 1 - i].charCodeAt(0)) -
Math.min(s[i].charCodeAt(0), s[len - 1 - i].charCodeAt(0));
var D2 = 26 - D1;
result += Math.min(D1, D2);
}
return result;
}
var s = "abccdb" ;
document.write(minOperations(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...