Count ways to Split a String into two Subsets that are Reverse of Each Other


In this tutorial, we delve into the problem of dividing a given string into two non-empty subsets, where the first subset is the reverse of the second subset. We aim to provide an efficient solution to count the number of ways to achieve such partitions. By leveraging the power of the C++ programming language, we present a solution that utilizes bitmasking and string manipulation techniques to iterate through all possible partitions and validate them against the given condition.

We will explore the step-by-step implementation of the solution, discussing the algorithm and code structure. Additionally, we will provide a comprehensive example to illustrate the usage of the solution in practice. By the end of this tutorial, readers will have a clear understanding of how to tackle this problem and obtain the desired count of valid partitions, empowering them to handle similar scenarios effectively in their own C++ programs.

Problem Statement

Given a string S of length N, the objective is to determine the count of possible ways to divide the string into two non-empty subsets in such a manner that the first subset is the reverse of the second subset.

Let’s understand this problem statement with examples!

Sample Example

Input

S = "cabaacba"

Output

Total count of valid subsets: 4

Explanation

There are four valid partitions that satisfy the given conditions:

Partition: {0, 4, 6, 7}
First Subset: "caba"
Second Subset: "abac" (reverse of the first subset)
Partition: {0, 3, 6, 7}
First Subset: "caba"
Second Subset: "abac" (reverse of the first subset)
Partition: {1, 2, 3, 5}
First Subset: "abac"
Second Subset: "caba" (reverse of the first subset)
Partition: {1, 2, 4, 5}
First Subset: "abac"
Second Subset: "caba" (reverse of the first subset)
Hence, the total count of valid partitions for the given string "cabaacba" is 4.

Input

N = 11, S = "mippiisssisssiipsspiim"

Output

Total count of valid subsets: 504

Explanation

For the given string "mippiisssisssiipsspiim", there are 504 valid partitions that satisfy the given conditions.

The partitions are obtained by selecting the indices that form the first subset, while the remaining characters form the second subset. The reverse of the first subset should match the second subset.

The count of valid partitions is 504, which represents the total number of ways the string can be split into two subsets meeting the given conditions.

Algorithm

STEP 1. Start by defining the ‘countReverseSubsets’ function that takes a string ‘str’ as input and initializes the count variable to 0.

STEP 2. Iterate over all possible partitions using a bitmask. The bitmask represents the subsets where each bit corresponds to a character in the string.

STEP 3. For each partition, separate the characters into the first and second subsets based on the bitmask.

STEP 4. Reverse the second subset and check if it is equal to the first subset. If they are equal, increment the count.

STEP 5. Return the count as the total number of valid partitions.

STEP 6. In the ‘main’ function, provide an example string as input, call the ‘countReverseSubsets’ function, and print the result.

Note: This algorithm utilizes bitmasking to generate all possible partitions of the string efficiently, checking each partition for validity. By comparing the subsets and counting the valid partitions, it determines the total count satisfying the given conditions.

Now, after understanding the algorithm, let’s execute it using C++ with the help of an example.

Example

Implementation of the above algorithm using C++

The below C++ program counts the number of ways to partition a given string into two non-empty subsets, satisfying the condition that the first subset is the reverse of the second subset. It iterates through all possible partitions by using a bitmask to represent the subsets. For each partition, it checks if the first subset is equal to the reverse of the second subset. Finally, it returns the total count of valid partitions.

Input

"cabaacba"

Output

Total count of valid subsets: 4

Example

#include <iostream>
#include <string>
#include <algorithm>
int countReverseSubsets(const std::string& str) {
   int count = 0;
   int n = str.length();
   for (int mask = 0; mask < (1 << n); mask++) {
      std::string firstSubset, secondSubset;
      for (int i = 0; i < n; i++) {
         if (mask >> i & 1) {
            firstSubset += str[i];
         } else {
            secondSubset += str[i];
         }
      }
      std::string reversedSubset = secondSubset;
      std::reverse(reversedSubset.begin(), reversedSubset.end());
      if (firstSubset == reversedSubset) {
         count++;
      }
   }
   return count;
}
int main() {
   std::string inputString = "cabaacba";
   int result = countReverseSubsets(inputString);
   std::cout << "Total count of valid subsets: " << result << std::endl;
   return 0;
}

Output

Total count of valid subsets: 4

Conclusion

To sum up, we have explored the problem of counting ways to split a string into two subsets that are in reverse of each other. By employing the power of C++ programming, we have provided an efficient solution that utilizes bitmasking and string manipulation techniques. Through the step-by-step explanation of the algorithm and code implementation, we have demonstrated how to solve this problem effectively. Happy Learning!!

Updated on: 08-Sep-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements