Created
November 24, 2014 10:56
-
-
Save rohith2506/90120beee18e46d5c3f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Simple logic:- | |
if open > close: call close one and increase its count | |
if open < n: call open one and increase its count | |
breaking point: when close == n | |
http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/ | |
*/ | |
class Solution { | |
public: | |
void permute(string res, int n, int open, int close){ | |
if(close == n){ | |
result.push_back(res); | |
return ; | |
} | |
else{ | |
if(open > close) | |
permute(res+")",n,open,close+1); | |
if(open < n) | |
permute(res+"(",n,open+1,close); | |
} | |
} | |
vector<string> generateParenthesis(int n) { | |
string res = ""; | |
permute(res,n,0,0); | |
return result; | |
} | |
private: | |
vector<string> result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment