Count of ways to split given string into two non | Java program to implement the above approach ; Function to check whether the substring from l to r is palindrome or not ; If characters at l and r differ ; Not a palindrome ; If the string is a palindrome ; Function to count and return the number of possible splits ; Stores the count of splits ; Check if the two substrings after the split are palindromic or not ; If both are palindromes ; Print the final count ; Driver Code
class GFG { public static boolean isPalindrome ( int l , int r , String s ) { while ( l <= r ) { if ( s . charAt ( l ) != s . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } public static int numWays ( String s ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isPalindrome ( 0 , i , s ) && isPalindrome ( i + 1 , n - 1 , s ) ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { String S = " aaaaa " ; System . out . println ( numWays ( S ) ) ; } }