[InterviewBit] Generate all Parentheses II

Generate all Parentheses II

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void helper(string& s, int open, int fin, vector<string>& res) {
if(open == 0 and fin == 0) res.push_back(s);
else {
if(open < fin) {
s.push_back('(');
helper(s,open + 1, fin, res);
s.pop_back();
}
if(open) {
s.push_back(')');
helper(s,open - 1, fin - 1, res);
s.pop_back();
}
}
}
vector<string> Solution::generateParenthesis(int A) {
vector<string> res;
helper(string() = "", 0, A, res);
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/22/PS/interviewbit/generate-all-parentheses-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.