Check if a string can be split into even length palindromic substrings | Java program for the above approach ; Function to check String str can be split a String into even length palindromic subStrings ; Initialize a stack ; Iterate the String ; If the i - th character is same as that at the top of the stack then pop the top element ; Else push the current charactor into the stack ; If the stack is empty , then even palindromic subStrings are possible ; Else not - possible ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static boolean check ( String s , int n ) { Stack < Character > st = new Stack < Character > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! st . isEmpty ( ) && st . peek ( ) == s . charAt ( i ) ) st . pop ( ) ; else st . add ( s . charAt ( i ) ) ; } if ( st . isEmpty ( ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { String str = " aanncddc " ; int n = str . length ( ) ; if ( check ( str , n ) ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } }