Posted on

Description

Submission

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        generateParenthesisUtil(n, n, "", res);
        return res;
    }
    
private:
    void generateParenthesisUtil(int left, int right, string str, vector<string>& res)
    {
        if(left == 0 && right == 0) res.push_back(str);
        if(left > 0) generateParenthesisUtil(left - 1, right, str + "(", res);
        if(right > 0 && left < right) generateParenthesisUtil(left, right - 1, str + ")", res);
    }
};

Leave a Reply

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