Check if string is palindrome after removing all consecutive duplicates | Function to check if a string is palindrome or not ; Length of the string ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as Str is palindromic ; Function to check if string str is palindromic after removing every consecutive characters from the str ; Length of the string str ; Create an empty compressed string ; The first character will always be included in final string ; Check all the characters of the string ; If the current character is not same as its previous one , then insert it in the final string ; Check if the compressed string is a palindrome ; Driver Code ; Given string ; Function call
def isPalindrome ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( Str [ i ] != Str [ Len - i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isCompressablePalindrome ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE compressed = " " NEW_LINE compressed += Str [ 0 ] NEW_LINE for i in range ( 1 , Len ) : NEW_LINE INDENT if ( Str [ i ] != Str [ i - 1 ] ) : NEW_LINE INDENT compressed += Str [ i ] NEW_LINE DEDENT DEDENT return isPalindrome ( compressed ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " abbcbbbaaa " NEW_LINE if ( isCompressablePalindrome ( Str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT