Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Functions checks if characters at odd index of the String forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str . charAt ( left ) != str . charAt ( right ) ) return false ; left += 2 ; right -= 2 ; } return true ; } 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"); } }