Generate Parentheses

 

CREATED TIME: Feb 17, 2020 10:39 PM UPDATED TIME: Feb 17, 2020 10:40 PM 난이도: Medium

class Solution { public: void solve(string s, int left, int right, vector& ans, int limit){ if(s.length() == limit * 2){ ans.push_back(s); return; } if(left < limit) solve(s + "(", left+1, right, ans, limit); if(right < left) solve(s + ")", left, right+1, ans, limit);

    }
    vector<string> generateParenthesis(int n) {
        vector<string> ans;
        solve("", 0, 0, ans, n);
        return ans;
    }
};