Generate All Valid Combinations of n Pairs of Parentheses in C++


#include <iostream> 
#include <vector> 
#include <string>

class ParenthesesGenerator {
public:
    // Public method to initiate the generation process
    std::vector generateParenthesis(int n) {
        std::vector result;
        generate("", n, n, result);
        return result;
    }

private:
    // Recursive helper function to generate combinations
    void generate(std::string current, int open, int close, std::vector& result) {
        // If there are no more open or close parentheses to add
        if (open == 0 && close == 0) {
            result.push_back(current);
            return;
        }

        // If we can still add an open parenthesis
        if (open > 0) {
            generate(current + "(", open - 1, close, result);
        }

        // If we can add a close parenthesis (only if there are open ones to match)
        if (close > open) {
            generate(current + ")", open, close - 1, result);
        }
    }
};

int main() {
    ParenthesesGenerator generator;
    int n;

    // User input for the number of pairs of parentheses
    std::cout << "Enter the number of pairs of parentheses: "; std::cin >> n;

    // Generate and display the valid combinations
    std::vector combinations = generator.generateParenthesis(n);
    
    std::cout << "Valid combinations of " << n << " pairs of parentheses are:\n";
    for (const std::string& combination : combinations) {
        std::cout << combination << std::endl;
    }

    return 0;
}
    

Program Explanation

This C++ program generates all valid combinations of n pairs of parentheses using a recursive approach. The key components of the program are:

1. Class Structure

The ParenthesesGenerator class contains two methods:

  • generateParenthesis(int n): This is the public method that initializes the process of generating parentheses combinations. It takes an integer n as input, which represents the number of pairs of parentheses.
  • generate(std::string current, int open, int close, std::vector& result): This private helper method performs the actual generation of combinations using recursion. It builds the current combination of parentheses and tracks the remaining open and close parentheses.

2. Recursive Logic

The recursion works as follows:

  • If both open and close counters reach zero, the current combination is complete, and it is added to the result list.
  • If open is greater than zero, it means we can add an open parenthesis ( and the method calls itself with updated counters.
  • We can add a close parenthesis ) only if there are open parentheses available (i.e., close > open).

3. User Input

The main function prompts the user to enter the number of pairs of parentheses. It then calls the generateParenthesis method and prints the resulting combinations.

Usage

To use the program, compile and run it, then input the number of pairs of parentheses when prompted. The program will output all valid combinations.

 

One Reply to “Generate All Valid Combinations of n Pairs of Parentheses in C++”

Leave a Reply

Your email address will not be published. Required fields are marked *