Parentheses generated are right but not in the same order as output.


  • 0
    J

    My solution is giving exactly the same number of parentheses but not in the same order as the output. Shouldn't the output checker just check the validity of individual parentheses rather than matching letter by letter.

    Here is my code if anybody wants to point out something fishy.

    vector<string> generateParenthesis(int n) {
            vector<string> res;
            if (n==0)
                return res;
            if (n==1) {
                res.push_back("()");
                return res;
            }
            
            vector<string> sub_prob = generateParenthesis(n-1); 
            for(int i=0;i<sub_prob.size();i++) {
                string left = "()"+sub_prob[i];
                string center = "("+sub_prob[i]+")";
                string right = sub_prob[i]+"()";
                res.push_back(center);
                if (left!=right) {
                    res.push_back(left);
                    res.push_back(right);
                }
                else {
                    res.push_back(left); //since left and right are the same
                }
            }
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.