计算将字符串拆分为彼此相反的两个子集的方法
给定一个由N个字符组成的字符串S ,任务是找到将字符串分成两个子集的方法数,使得第一个子集与第二个子集相反。
例子:
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
方法:可以通过使用位掩码的概念来解决给定的问题,以生成所有可能的字符串拆分方式,并检查是否存在任何这种相互反转的字符串拆分。请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0以存储对字符串进行分区的方式总数。
- 使用变量掩码迭代范围[0, 2 N ]并执行以下步骤:
- 初始化两个字符串X和Y来存储第一个子集和第二个子集的字符。
- 遍历[0, N]范围,如果在整数掩码中设置了第 i位,则将字符S[i]附加到X 。否则将字符S[i]附加到Y 。
- 反转字符串Y然后检查第一个字符串X是否等于第二个字符串Y然后将ans递增1 。
- 完成上述步骤后,将ans的值打印为总路数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the total number
// of ways to partitiaon the string into
// two subset satisfying the conditions
int countWays(string S, int N)
{
// Stores the resultant number of
// ways of splitting
int ans = 0;
// Iterate over the range [0, 2^N]
for (int mask = 0;
mask < (1 << N); mask++) {
string X, Y;
// Traverse the string S
for (int i = 0; i < N; i++) {
// If ith bit is set, then
// append the character
// S[i] to X
if (mask >> i & 1) {
X += S[i];
}
// Otherwise, append the
// character S[i] to Y
else {
Y += S[i];
}
}
// Reverse the second string
reverse(Y.begin(), Y.end());
// If X is equal to Y
if (X == Y) {
ans++;
}
}
// Return the total number of ways
return ans;
}
// Driver Code
int main()
{
string S = "mippiisssisssiipsspiim";
int N = S.length();
cout << countWays(S, N);
return 0;
}
Java
// Java program for the above approach
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG {
// Function to find the total number
// of ways to partitiaon the String into
// two subset satisfying the conditions
static int countWays(String S, int N)
{
// Stores the resultant number of
// ways of splitting
int ans = 0;
// Iterate over the range [0, 2^N]
for (int mask = 0;
mask < (1 << N); mask++) {
String X="" , Y="";
// Traverse the String S
for (int i = 0; i < N; i++) {
// If ith bit is set, then
// append the character
// S[i] to X
if ((mask >> i & 1) == 1) {
X += S.charAt(i);
}
// Otherwise, append the
// character S[i] to Y
else {
Y += S.charAt(i);
}
}
// Reverse the second String
Y = new StringBuilder(Y).reverse().toString();
// If X is equal to Y
if (X.equals(Y)) {
ans++;
}
}
// Return the total number of ways
return ans;
}
// Driver Code
public static void main (String[] args)
{
String S = "mippiisssisssiipsspiim";
int N = S.length();
System.out.println(countWays(S, N));
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 program for the above approach
# Function to find the total number
# of ways to partitiaon the string into
# two subset satisfying the conditions
def countWays(S, N):
# Stores the resultant number of
# ways of splitting
ans = 0
# Iterate over the range [0, 2^N]
for mask in range((1 << N)):
X, Y = "",""
# Traverse the string S
for i in range(N):
# If ith bit is set, then
# append the character
# S[i] to X
if (mask >> i & 1):
X += S[i]
# Otherwise, append the
# character S[i] to Y
else:
Y += S[i]
# Reverse the second string
Y = Y[::-1]
# If X is equal to Y
if (X == Y):
ans += 1
# Return the total number of ways
return ans
# Driver Code
if __name__ == '__main__':
S = "mippiisssisssiipsspiim"
N = len(S)
print(countWays(S, N))
# This code is contributed by mohit kumar 29
Javascript
输出:
504
时间复杂度: O(N*2 N )
辅助空间: O(N)