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”.
Input: str = “abbcbbbaaa”
On removing all consecutive duplicates characters, the string becomes “abcba” which is a palindrome.
Input: str = “aaabbbaaccc”
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:
Time Complexity: O(N), where N is length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if string remains palindrome after removing given number of characters
- Character replacement after removing duplicates from a string
- Remove all consecutive duplicates from the string
- Remove three consecutive duplicates from string
- Reduce the string by removing K consecutive identical characters
- Find longest palindrome formed by removing or shuffling chars from string
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Check if a given string is Even-Odd Palindrome or not
- Check given string is oddly palindrome or not | Set 2
- C Program to Check if a Given String is Palindrome
- Check given string is oddly palindrome or not
- Check whether the given string is Palindrome using Stack
- Check if a string is palindrome in C using pointers
- Check if it is possible to create a palindrome string from given N
- Check if both halves of a string are Palindrome or not
- Check if any anagram of a string is palindrome or not
- Check if a given string is a rotation of a palindrome
- Check if a string has m consecutive 1's or 0's
- Check if there exists any sub-sequence in a string which is not palindrome
- Python program to check if a string is palindrome or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Amal Kumar Choubey