Minimum number of operations to make string palindrome

Concepts Used

Strings

Difficulty Level

Easy

Problem Statement (Simplified):

Convert given string into a palindrome by decreasing or increasing characters by their ASCII value, in minimum number of operations. Each decrement or increment is considered as one opeartion. Print the number of operations performed.

See original problem statement here

Test Case

Input:
1
abcd

Output:
4

Explanation:
String 'abcd' can be converted into its palindrome if we convert : 

'a'->'d' or 'd'->'a'  which takes (ASCII(d) - ASCII(a)) steps i.e. 3 steps.

'b'->'c' or 'c'->'b' which takes (ASCII(b) - ASCII(c)) steps i.e. 1 step.

Hence the total number of steps is 4.

Solving Approach :

1) Palindrome: A palindrome is a string that contains same characters from both ends. For example, nitin can be read same from both ends. Element at ith index from the start and end side both are the same.

2) As we know palindromes contains same characters at ith index and (len -i)th index. So we iterate from 0 to middle of the string and compare all the characters if at some index characters are different, the string is not palindrome. If all the characters remain same, the string is palindrome.

3) If two characters are not same at ith index and (len -i)th index, we have to make them the same. We can make them same by adding the ASCII value of difference of both the character to the smaller character.

4) Finally we add the difference to our counter, the final value of the counter will be answer according to the fundamentals of data structures in c++.

Example:

  • Let's assume given string is abcds and we check values from both ends one by one, and check their difference, we take positive difference value as our step.
  • In 1st iteration, we compare a from start and s from end, we can convert either a to s or s to a. Their ASCII value difference is 18, so our current step count is 18.
  • In 2nd iteration, we compare b from start and d from end, we can convert either b to d or d to b. Their ASCII value difference is 2, so our current step count is 20.
  • As last element left is middle element,which is also a palindrome in it's own, so we'll skip that. Hence We get our final count as 20.

Solutions:

#include 
int main()
{
  int test;
  scanf("%d", &test);

  while(test--){

    char a[10000001];
    scanf("%s", a);

    int no_of_operations = 0;

    for(int i=0; i


						 
#include 
using namespace std;
int main()
{
  int test;
  cin>>test;

  while(test--){

    char a[10000001];
    cin>>a;

    int no_of_operations = 0;

    for(int i=0; i


						 
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
  public static void main(String args[]) throws IOException {

    Scanner sc= new Scanner(System.in);
    int test = sc.nextInt();
      while(test != 0){

        String a = sc.next();

        int no_of_operations = 0;

        for(int i=0; i

						 
for _ in range(int(input())):

    a = input()

    no_of_operations = 0

    for i in range(len(a)//2):
        no_of_operations += abs( ord(a[len(a)-i-1]) - ord(a[i]) )
    
    print(no_of_operations)

#include 

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
int main(void) {
	// your code goes here
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int start[n];
		int end[n];
		
		for(int i=0;i= st)
			{
				ans++;
				st = start[i];
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

[forminator_quiz id="936"]

This article tried to discuss the concept of Strings. Hope this blog helps you understand and solve the problem. To practice more problems on Strings you can check out MYCODE | Competitive Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *