📌  相关文章
📜  计算将字符串拆分为彼此相反的两个子集的方法

📅  最后修改于: 2022-05-13 01:56:07.906000             🧑  作者: Mango

计算将字符串拆分为彼此相反的两个子集的方法

给定一个由N个字符组成的字符串S ,任务是找到将字符串分成两个子集的方法数,使得第一个子集与第二个子集相反。

例子:

方法:可以通过使用位掩码的概念来解决给定的问题,以生成所有可能的字符串拆分方式,并检查是否存在任何这种相互反转的字符串拆分。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0以存储对字符串进行分区的方式总数。
  • 使用变量掩码迭代范围[0, 2 N ]并执行以下步骤:
    • 初始化两个字符串XY来存储第一个子集和第二个子集的字符。
    • 遍历[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)