Check if string is palindrome after removing all consecutive duplicates | Java program for the above approach ; 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 the 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
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 ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }