Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Function to check if String formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic String is not compared ; If stack is empty then return true ; Driver Code ; Given String
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; boolean lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < Character > s = new Stack < Character > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . add ( str . charAt ( i ) ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . peek ( ) == str . charAt ( i ) ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }