Count ways to split a string into two subsets that are reverse of each other | Java program for the above approach ; Function to find the total number of ways to partitiaon the String into two subset satisfying the conditions ; Stores the resultant number of ways of splitting ; Iterate over the range [ 0 , 2 ^ N ] ; Traverse the String S ; If ith bit is set , then append the character S [ i ] to X ; Otherwise , append the character S [ i ] to Y ; Reverse the second String ; If X is equal to Y ; Return the total number of ways ; Driver Code
import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { static int countWays ( String S , int N ) { int ans = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { String X = " " , Y = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( mask >> i & 1 ) == 1 ) { X += S . charAt ( i ) ; } else { Y += S . charAt ( i ) ; } } Y = new StringBuilder ( Y ) . reverse ( ) . toString ( ) ; if ( X . equals ( Y ) ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String S = " mippiisssisssiipsspiim " ; int N = S . length ( ) ; System . out . println ( countWays ( S , N ) ) ; } }