Skip to content

Commit

Permalink
Added Generate Parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Sep 3, 2023
1 parent f464c9a commit 5e742a9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Medium/GenerateParentheses/include/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <gtest/gtest.h>

class Solution {
public:
static auto generate_parenthesis(const int32_t n) -> std::vector<std::string> {

std::vector<std::string> result;

std::function<void(std::string, int32_t, int32_t)> backtrack = [&](const std::string &current,
const int32_t open, const int32_t close) -> void {
if (current.length() == n * 2) {
result.emplace_back(current);
return;
} if (open < n) {
backtrack(current + '(', open + 1, close);
} if (close < open) {
backtrack(current + ')', open, close + 1);
}
};

backtrack({ }, 0, 0);
return result;
}
};

0 comments on commit 5e742a9

Please sign in to comment.