- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimize operations to make String palindrome by incrementing prefix by 1
In this problem, we will count the number of operations required by increasing the prefix characters of the given string.
We will use character difference to count the minimum number of operations required to make string palindromic.
Problem Statement
We have given a string nums containing the numeric digits. We need to count a minimum number of operations required to convert a string into the palindrome.
In one operation, we can select any prefix of the string and increment all prefix characters by 1.
Sample Example
Input
nums = "22434"
Output
2
Explanation
First, we can choose 22 prefixes and increment all characters. So, the string becomes 33434.
After that, we can choose the '3' prefix, and the string becomes 43434, which is a palindromic string.
Input
nums = '151'
Output
0
Explanation - The string is already palindromic. So, it prints 0.
Input
nums = "32102"
Output
-1
Explanation - It is not possible to convert the string to palindromic by incrementing the prefix values.
Approach 1
We can make the string palindromic according to the problem statement if the string follows the below 2 conditions.
After dividing the string into two equal parts, the digits of the first part should be smaller than the second.
In the left part, starting characters should be greater than the ending character as we need to choose any prefix and increment each character by 1.
Algorithm
Step 1 − Initialize the q with len - 1 and p with 0, as we use them as an index pointers. Initialize the maxOps with a maximum integer value to store the minimum operations and 'curr' with 0 to store the maximum difference.
Step 2 − Start traversing the string until q > p.
Step 3 − If the character at index q is less than index p, return -1, as converting the string to palindromic is impossible.
Step 4 − Store the difference of ASCII values of characters at index q and p into the 'diff' variable.
Step 5 − Store the maximum of the 'curr' and 'diff' in the' curr' variable.
Step 6 − If the 'maxOps' value is less than 'diff', return -1.
Step 7 − Update the 'maxOps' with the 'diff' value.
Step 8 − Increment p by 1 and decrement q by 1.
Step 9 − Return the 'curr' value.
Example
#include <stdio.h> #include <string.h> #include <limits.h> int makePalindrome(char alpha[], int len) { int q = len - 1; int p = 0; int maxOpes = INT_MAX; int curr = 0; // Traverse from both ends while (q > p) { // It is not possible to make the string palindromic if (alpha[q] < alpha[p]) { return -1; } // Get character difference int diff = alpha[q] - alpha[p]; // Get the maximum current difference curr = (curr > diff) ? curr : diff; // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } int main() { char nums[] = "22434"; int len = strlen(nums); printf("The number of minimum operations required to make the string palindromic is %d\n", makePalindrome(nums, len)); return 0; }
Output
The number of minimum operations required to make the string palindromic is 2
#include <bits/stdc++.h> using namespace std; int makePalindrome(string alpha, int len) { int q = len - 1; int p = 0; int maxOpes = INT_MAX; int curr = 0; // Travere from both ends while (q > p) { // It is not possible to make string palindromic if (alpha[q] < alpha[p]) { return -1; } // Get character difference int diff = alpha[q] - alpha[p]; // Get the maximum current difference curr = max(curr, diff); // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } int main() { string nums = "22434"; int len = nums.length(); cout << "The number of minimum operations required to make string palindromic is " << makePalindrome(nums, len); return 0; }
Output
The number of minimum operations required to make string palindromic is 2
public class Main { public static int makePalindrome(String alpha) { int len = alpha.length(); int q = len - 1; int p = 0; int maxOpes = Integer.MAX_VALUE; int curr = 0; // Traverse from both ends while (q > p) { // It is not possible to make the string palindromic if (alpha.charAt(q) < alpha.charAt(p)) { return -1; } // Get character difference int diff = alpha.charAt(q) - alpha.charAt(p); // Get the maximum current difference curr = Math.max(curr, diff); // At the center side difference should be less than the characters at the end if (maxOpes < diff) { return -1; } maxOpes = diff; p++; q--; } return curr; } public static void main(String[] args) { String nums = "22434"; int len = nums.length(); System.out.println("The number of minimum operations required to make the string palindromic is " + makePalindrome(nums)); } }
Output
The number of minimum operations required to make the string palindromic is 2
def make_palindrome(alpha): q = len(alpha) - 1 p = 0 max_opes = float('inf') curr = 0 # Traverse from both ends while q > p: # It is not possible to make the string palindromic if alpha[q] < alpha[p]: return -1 # Get character difference diff = ord(alpha[q]) - ord(alpha[p]) # Get the maximum current difference curr = max(curr, diff) # At the center side difference should be less than the characters at the end if max_opes < diff: return -1 max_opes = diff p += 1 q -= 1 return curr nums = "22434" print(f"The number of minimum operations required to make the string palindromic is {make_palindrome(nums)}")
Output
The number of minimum operations required to make the string palindromic is 2
Time complexity − O(N) for traversing the string.
Space complexity − O(1), as we don't use any extra space.
In the solution, we check the difference from the start to the center and return -1 if any center-side characters have a higher difference. Programmers may try traversing the string from the center and checking for the higher difference at the starting side.
To Continue Learning Please Login
Login with Google