Check if string is palindrome after removing all consecutive duplicates
Last Updated :
03 Oct, 2022
Given a string str, the task is to remove all the consecutive duplicates from the string str and check if the final string is palindrome or not. Print “Yes” if it is a palindromic else print “No”.
Examples:
Input: str = “abbcbbbaaa”
Output: Yes
Explanation:
On removing all consecutive duplicates characters, the string becomes “abcba” which is a palindrome.
Input: str = “aaabbbaaccc”
Output: No
Explanation:
On removing all consecutive duplicates characters, the string becomes “abac” which is not a palindrome.
Approach: The idea is to create a new string from the given string and check if the new string is palindromic or not. Below are the steps:
- Initialise the new string newStr = “”.
- Iterate through all the characters of the given string one by one and if the current character is the different from the previous character, then append this character to the new string newStr.
- Else check for the next character.
- Check if the final string formed is palindrome or not. Print “Yes” if it is a palindromic else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string str)
{
int len = str.length();
for ( int i = 0; i < len; i++) {
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
bool isCompressablePalindrome(string str)
{
int len = str.length();
string compressed = "" ;
compressed.push_back(str[0]);
for ( int i = 1; i < len; i++) {
if (str[i] != str[i - 1])
compressed.push_back(str[i]);
}
return isPalindrome(compressed);
}
int main()
{
string str = "abbcbbbaaa" ;
if (isCompressablePalindrome(str))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPalindrome(String str)
{
int len = str.length();
for ( int i = 0 ; i < len; i++)
{
if (str.charAt(i) != str.charAt(len - i - 1 ))
return false ;
}
return true ;
}
static boolean isCompressablePalindrome(String str)
{
int len = str.length();
String compressed = "" ;
compressed = String.valueOf(str.charAt( 0 ));
for ( int i = 1 ; i < len; i++)
{
if (str.charAt(i) != str.charAt(i - 1 ))
compressed += str.charAt(i);
}
return isPalindrome(compressed);
}
public static void main(String[] args)
{
String str = "abbcbbbaaa" ;
if (isCompressablePalindrome(str))
System.out.print( "Yes\n" );
else
System.out.print( "No\n" );
}
}
|
Python3
def isPalindrome( Str ):
Len = len ( Str )
for i in range ( Len ):
if ( Str [i] ! = Str [ Len - i - 1 ]):
return False
return True
def isCompressablePalindrome( Str ):
Len = len ( Str )
compressed = ""
compressed + = Str [ 0 ]
for i in range ( 1 , Len ):
if ( Str [i] ! = Str [i - 1 ]):
compressed + = Str [i]
return isPalindrome(compressed)
if __name__ = = '__main__' :
Str = "abbcbbbaaa"
if (isCompressablePalindrome( Str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPalindrome(String str)
{
int len = str.Length;
for ( int i = 0; i < len; i++)
{
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
static bool isCompressablePalindrome(String str)
{
int len = str.Length;
String compressed = "" ;
compressed = String.Join( "" , str[0]);
for ( int i = 1; i < len; i++)
{
if (str[i] != str[i - 1])
compressed += str[i];
}
return isPalindrome(compressed);
}
public static void Main(String[] args)
{
String str = "abbcbbbaaa" ;
if (isCompressablePalindrome(str))
Console.Write( "Yes\n" );
else
Console.Write( "No\n" );
}
}
|
Javascript
<script>
function isPalindrome(str)
{
let len = str.length;
for (let i = 0; i < len; i++)
{
if (str[i] != str[len - i - 1])
return false ;
}
return true ;
}
function isCompressablePalindrome(str)
{
let len = str.length;
let compressed = "" ;
compressed = str[0];
for (let i = 1; i < len; i++)
{
if (str[i] != str[i - 1])
compressed += str[i];
}
return isPalindrome(compressed);
}
let str = "abbcbbbaaa" ;
if (isCompressablePalindrome(str))
document.write( "Yes" + "</br>" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N) as extra space is required for string compressed.
Share your thoughts in the comments
Please Login to comment...