Check if given string can be made Palindrome by removing only single type of character
Last Updated :
30 Sep, 2022
Given a string S, the task is to whether a string can be made palindrome after removing the occurrences of the same character, any number of times
Examples:
Input: S = “abczdzacb”
Output: Yes
Explanation: Remove first and second occurrence of character ‘a’, string S becomes “bczdzcb”, which is a palindrome .
Input: S = “madem”
Output: No
Approach: The task can be solved by iterating over each unique character in the given string, and removing its occurrences wherever there is a mismatch, if a valid palindrome is found, after removing occurrences of the same character any number of times, return “Yes” else return “No“.
Follow the below steps to solve the problem:
- Start iterating over each unique character of the string, whose occurrences are to be deleted
- Use the two-pointer technique, to check for a mismatch, Place l at the start of the string and r at the end of the string
- If S[l] == S[r], increment l, and decrement r.
- If S[l]!= S[r], check if S[l[ == char, do l++, else if S[r] == char, do r–
- If none of the conditions hold means the given can’t be converted into a palindrome
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isPossible(string S)
{
int n = ( int )S.length();
set< char > st;
for ( int i = 0; i < n; i++) {
st.insert(S[i]);
}
bool check = false ;
for ( auto ele : st) {
int low = 0, high = n - 1;
bool flag = true ;
for ( int i = 0; i < n; i++) {
if (S[low] == S[high]) {
low++;
high--;
}
else {
if (S[low] == ele) {
low++;
}
else if (S[high] == ele) {
high--;
}
else {
flag = false ;
break ;
}
}
}
if (flag == true ) {
check = true ;
break ;
}
}
if (check)
return "Yes" ;
else
return "No" ;
}
int main()
{
string S = "abczdzacb" ;
cout << isPossible(S);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String isPossible(String S)
{
int n = S.length();
Set<Character> st = new HashSet<Character>();
for ( int i = 0 ; i < n; i++) {
st.add(S.charAt(i));
}
boolean check = false ;
for (Character ele : st) {
int low = 0 , high = n - 1 ;
boolean flag = true ;
for ( int i = 0 ; i < n; i++) {
if (S.charAt(low) == S.charAt(high)) {
low++;
high--;
}
else {
if (S.charAt(low) == ele) {
low++;
}
else if (S.charAt(high)== ele) {
high--;
}
else {
flag = false ;
break ;
}
}
}
if (flag == true ) {
check = true ;
break ;
}
}
if (check)
return "Yes" ;
else
return "No" ;
}
public static void main (String[] args) {
String S = "abczdzacb" ;
System.out.println(isPossible(S));
}
}
|
Python3
def isPossible(S):
n = len (S)
st = set ()
for i in range ( 0 , n):
st.add(S[i])
check = False
for ele in st:
low = 0
high = n - 1
flag = True
for i in range ( 0 , n):
if (S[low] = = S[high]):
low + = 1
high - = 1
else :
if (S[low] = = ele):
low + = 1
elif (S[high] = = ele):
high - = 1
else :
flag = False
break
if (flag = = True ):
check = True
break
if (check):
return "Yes"
else :
return "No"
if __name__ = = "__main__" :
S = "abczdzacb"
print (isPossible(S))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static String isPossible(String S)
{
int n = S.Length;
HashSet< char > st = new HashSet< char >();
for ( int i = 0; i < n; i++) {
st.Add(S[i]);
}
bool check = false ;
foreach ( char ele in st) {
int low = 0, high = n - 1;
bool flag = true ;
for ( int i = 0; i < n; i++) {
if (S[low] == S[high]) {
low++;
high--;
}
else {
if (S[low] == ele) {
low++;
}
else if (S[high]== ele) {
high--;
}
else {
flag = false ;
break ;
}
}
}
if (flag == true ) {
check = true ;
break ;
}
}
if (check)
return "Yes" ;
else
return "No" ;
}
public static void Main(String[] args) {
String S = "abczdzacb" ;
Console.WriteLine(isPossible(S));
}
}
|
Javascript
<script>
function isPossible(S)
{
let n = S.length;
let st = new Set();
for (let i = 0; i < n; i++) {
st.add(S[i]);
}
let check = false ;
for (ele of st) {
let low = 0, high = n - 1;
let flag = true ;
for (let i = 0; i < n; i++) {
if (S[low] == S[high]) {
low++;
high--;
}
else {
if (S[low] == ele) {
low++;
}
else if (S[high] == ele) {
high--;
}
else {
flag = false ;
break ;
}
}
}
if (flag == true ) {
check = true ;
break ;
}
}
if (check)
return "Yes" ;
else
return "No" ;
}
let S = "abczdzacb" ;
document.write(isPossible(S));
</script>
|
Time Complexity: O(n*26)
Auxiliary Space: O(1) because the size of set cannot exceed 26 if only lowercase alphabets are considered.
Share your thoughts in the comments
Please Login to comment...