Minimum operations to make a numeric string palindrome by removing at most 2 unique character occurrences
Last Updated :
14 Feb, 2022
Given numeric string str, the task is to find the minimum operations needed to make the string palindrome. An operation is defined as:
- Select one character and remove any of its occurrences.
- For all the operations, the number of unique characters chosen must be less than 2
Examples:
Input: str = “11221”
Output: 1
Explanation: Select ‘1’ and remove anyone character out of the first two characters of the given string. After the operation, the string becomes “1221” which is a palindrome.
Input: str = “1123211”
Output: 0
Explanation: The given string is already a palindrome.
Input: str = “1332”
Output: -1
Explanation: The string can’t be made palindrome after selecting a single character and removing the occurrences of the selected character only.
Approach: This problem can be solved with the help of the two-pointer approach.
Follow the steps below to solve the problem:
- Initialize a variable answer as n that stores our final answer (n is the length of the string).
- Iterate over ‘0’ to ‘9’ and let’s call it currentCharacter.
- Initialize toBeRemoved as 0. It stores the minimum number of characters to be removed of currentCharacter type from the string to make it a palindrome.
- Initialize i and j as 0 and n – 1 respectively.
- Initialize a possible as true. It helps us to determine whether it is possible to make the string palindrome by removing the occurrences of the character c only.
- Now run a nested while loop till i is less than j.
- Case 1: str[i] is equal to str[j], It means that we are not required to remove any character at the present step so we can increment i and decrement j.
- Case 2: str[i] is not equal to str[j] but str[i] is equal to currentCharacter. Increment i = i + 1 and toBeRemoved = toBeRemoved + 1 as we are removing this character.
- Case 3: str[i] is not equal to str[j] but str[j] is equal to currentCharacter. Increment j = j – 1 and toBeRemoved = toBeRemoved + 1 as we are removing this character
- Case 4: str[i] is not equal to str[j]. It means that we can’t make our string palindrome by removing the currentCharacter.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve(string str)
{
int n = str.length();
int answer = n;
for ( char currentCharacter = '0' ;
currentCharacter <= '9' ;
currentCharacter++) {
int i = 0, j = n - 1;
int toBeRemoved = 0;
bool possible = true ;
while (i < j) {
if (str[i] == str[j]) {
i++, j--;
}
else if (str[i] != str[j]
&& str[i] == currentCharacter) {
toBeRemoved++;
i++;
}
else if (str[i] != str[j]
&& str[j] == currentCharacter) {
toBeRemoved++;
j--;
}
else {
possible = false ;
break ;
}
}
if (possible)
answer = min(answer, toBeRemoved);
}
if (answer == n)
cout << "-1" ;
else
cout << answer;
}
int main()
{
string str = "11221" ;
solve(str);
}
|
Java
import java.io.*;
class GFG
{
static void solve(String str)
{
int n = str.length();
int answer = n;
for ( char currentCharacter = '0' ;
currentCharacter <= '9' ;
currentCharacter++) {
int i = 0 , j = n - 1 ;
int toBeRemoved = 0 ;
boolean possible = true ;
while (i < j) {
if (str.charAt(i) == str.charAt(j)) {
i++; j--;
}
else if (str.charAt(i) != str.charAt(j)
&& str.charAt(i) == currentCharacter) {
toBeRemoved++;
i++;
}
else if (str.charAt(i) != str.charAt(j)
&& str.charAt(j) == currentCharacter) {
toBeRemoved++;
j--;
}
else {
possible = false ;
break ;
}
}
if (possible)
answer = Math.min(answer, toBeRemoved);
}
if (answer == n)
System.out.println( "-1" );
else
System.out.println(answer);
}
public static void main (String[] args)
{
String str = "11221" ;
solve(str);
}
}
|
Python3
def solve( str ):
n = len ( str )
answer = n;
currentCharacter = '0'
while (currentCharacter < = '9' ):
i = 0
j = n - 1
toBeRemoved = 0
possible = True
while (i < j):
if ( str [i] = = str [j]):
i + = 1
j - = 1
elif ( str [i] ! = str [j] and str [i] = = currentCharacter):
toBeRemoved + = 1
i + = 1
elif ( str [i] ! = str [j] and str [j] = = currentCharacter):
toBeRemoved + = 1
j - = 1
else :
possible = False ;
break ;
currentCharacter = f "{int(currentCharacter) + 1}"
if (possible):
answer = min (answer, toBeRemoved);
if (answer = = n):
print ( "-1" );
else :
print (answer);
str = "11221" ;
solve( str );
|
C#
using System;
class GFG
{
static void solve( string str)
{
int n = str.Length;
int answer = n;
for ( char currentCharacter = '0' ;
currentCharacter <= '9' ;
currentCharacter++) {
int i = 0, j = n - 1;
int toBeRemoved = 0;
bool possible = true ;
while (i < j) {
if (str[i] == str[j]) {
i++;
j--;
}
else if (str[i] != str[j]
&& str[i] == currentCharacter) {
toBeRemoved++;
i++;
}
else if (str[i] != str[j]
&& str[j] == currentCharacter) {
toBeRemoved++;
j--;
}
else {
possible = false ;
break ;
}
}
if (possible)
answer = Math.Min(answer, toBeRemoved);
}
if (answer == n)
Console.Write( "-1" );
else
Console.Write(answer);
}
public static void Main()
{
string str = "11221" ;
solve(str);
}
}
|
Javascript
<script>
function solve(str)
{
let n = str.length;
let answer = n;
for (let currentCharacter = '0' ;
currentCharacter <= '9' ;
currentCharacter++) {
let i = 0, j = n - 1;
let toBeRemoved = 0;
let possible = true ;
while (i < j) {
if (str[i] == str[j]) {
i++;
j--;
}
else if (str[i] != str[j]
&& str[i] == currentCharacter) {
toBeRemoved++;
i++;
}
else if (str[i] != str[j]
&& str[j] == currentCharacter) {
toBeRemoved++;
j--;
}
else {
possible = false ;
break ;
}
}
if (possible)
answer = Math.min(answer, toBeRemoved);
}
if (answer == n)
document.write( "-1" );
else
document.write(answer);
}
let str = "11221" ;
solve(str);
</script>
|
Time complexity: O(10 * n) = O(n) where n is the length of the string str.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...