leetcode new problems

This commit is contained in:
2026-02-11 14:48:41 +05:30
parent 192ce44a81
commit 0f52d46c67
29 changed files with 658 additions and 5 deletions
+25
View File
@@ -0,0 +1,25 @@
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
dfs(0,0, "", n,res);
return res;
}
void dfs(int openP, int closeP, string s, int n, vector<string> & res){
if(openP == closeP && openP+closeP == 2*n){
res.push_back(s);
return;
}
if(openP<n){
dfs(openP+1, closeP, s+'(', n ,res);
}
if(closeP<openP){
dfs(openP, closeP+1, s+')',n,res);
}
}
};
//The recursion goes as deep as possible by completing one full parentheses string before coming back and trying another option.
//Each recursive call explores one path fully, then backtracks — which is exactly depth-first search
//Note we are passing s by value, so no need to reset it for every path