-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchedParen.h
More file actions
40 lines (37 loc) · 1.15 KB
/
MatchedParen.h
File metadata and controls
40 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// MatchedParen.h
// Recursion
//
// Created by shashank hegde on 12/20/15.
// Copyright (c) 2015 shashank hegde. All rights reserved.
//
#ifndef Recursion_MatchedParen_h
#define Recursion_MatchedParen_h
#include <vector>
#include <string>
using namespace std;
void generateBalancedParanthesisHelper(int num_left_pairs_neeeded, int num_right_pairs_needed,
const string valid_prefix, vector<string>* result )
{
if(!num_left_pairs_neeeded && !num_right_pairs_needed)
{
result->emplace_back(valid_prefix);
return;
}
if(num_left_pairs_neeeded)
{
generateBalancedParanthesisHelper(num_left_pairs_neeeded-1, num_right_pairs_needed, valid_prefix + '(', result);
}
if(num_left_pairs_neeeded < num_right_pairs_needed)
{
generateBalancedParanthesisHelper(num_left_pairs_neeeded, num_right_pairs_needed-1,
valid_prefix + ')',result);
}
}
vector<string> generateBalancedParanthesis(int num_pairs)
{
vector<string> result;
generateBalancedParanthesisHelper(num_pairs, num_pairs, "", &result);
return result;
}
#endif