Count ways to split a string into two subsets that are reverse of each other | 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
def countWays ( S , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for mask in range ( ( 1 << N ) ) : NEW_LINE INDENT X , Y = " " , " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( mask >> i & 1 ) : NEW_LINE INDENT X += S [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT Y += S [ i ] NEW_LINE DEDENT DEDENT Y = Y [ : : - 1 ] NEW_LINE if ( X == Y ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " mippiisssisssiipsspiim " NEW_LINE N = len ( S ) NEW_LINE print ( countWays ( S , N ) ) NEW_LINE DEDENT