Given a string S consisting of N characters, the task is to find the number of ways to split the string into two subsets such that the first subset is the reverse of the second subset.
Examples:
Input: S = “cabaacba”
Output: 4
Explanation:
Below are the ways of partitioning the string satisfying the given conditions:
- Take the characters at the indices {0, 4, 6, 7} as the first subset and the remaining characters as the second subset. Then the string formed are “caba” and “abac” and the second string is the reverse of the first string.
- Take the characters at the indices {0, 3, 6, 7} as the first subset and remaining characters as the second subset. Then the string formed are “caba” and “abac” and the second string is the reverse of the first string.
- Take the characters at the indices {1, 2, 3, 5} as first subset and remaining characters as the second subset. Then the string formed are “abac” and “caba” and the second string is the reverse of first string.
- Take the characters at the indices {1, 2, 4, 5} as first subset and remaining characters as the second subset. Then the string formed are “abac” and “caba” and the second string is the reverse of the first string.
Therefore, the number of ways of splitting is 4.
Input: N = 11, S = “mippiisssisssiipsspiim”
Output: 504
Approach: The given problem can be solved by using the concept of Bitmasking to generate all possible ways of splitting the string and check if there exists any such splitting of the string which is reverse of one another. Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0 to store the total number of ways of partitioning the string.
- Iterate over the range [0, 2N] using a variable mask and perform the following steps:
- Initialize two strings say X and Y to store the characters of the first subset and second subset.
- Iterate over the range [0, N] and if the ith bit is set in the integer mask then append the character S[i] to X. Otherwise append the character S[i] to Y.
- Reverse the string Y and then check if the first string X is equal to the second string Y then increment ans by 1.
- After completing the above steps, print the value of ans as the total number of ways.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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) {
X += S[i];
}
else {
Y += S[i];
}
}
reverse(Y.begin(), Y.end());
if (X == Y) {
ans++;
}
}
return ans;
}
int main()
{
string S = "mippiisssisssiipsspiim" ;
int N = S.length();
cout << countWays(S, N);
return 0;
}
|
Java
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));
}
}
|
Python3
def countWays(S, N):
ans = 0
for mask in range (( 1 << N)):
X, Y = " "," "
for i in range (N):
if (mask >> i & 1 ):
X + = S[i]
else :
Y + = S[i]
Y = Y[:: - 1 ]
if (X = = Y):
ans + = 1
return ans
if __name__ = = '__main__' :
S = "mippiisssisssiipsspiim"
N = len (S)
print (countWays(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static string Reverse( string s )
{
char [] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
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[i];
}
else {
Y += S[i];
}
}
Y = Reverse(Y);
if (X.Equals(Y)) {
ans++;
}
}
return ans;
}
public static void Main ( string [] args)
{
string S = "mippiisssisssiipsspiim" ;
int N = S.Length;
Console.WriteLine(countWays(S, N));
}
}
|
Javascript
<script>
function countWays(S, N){
let ans = 0
for (let mask=0;mask<(1 << N);mask++){
let X = ""
let Y = ""
for (let i=0;i<N;i++){
if (mask >> i & 1)
X += S[i]
else
Y += S[i]
}
Y = Y.split( "" ).reverse().join( "" )
if (X == Y)
ans += 1
}
return ans
}
let S = "mippiisssisssiipsspiim"
let N = S.length
document.write(countWays(S, N))
</script>
|
Time Complexity: O(N*2N)
Auxiliary Space: O(N)