Created
October 24, 2017 17:55
-
-
Save hoanbka/e8d3e5b67b0063905f3875e1e483a20f 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
def generateParenthesis(n): | |
list = [] | |
backtrack(list, "", 0, 0, n) | |
return list | |
def backtrack(list, str, open, close, max): | |
if (len(str) == max * 2): | |
list.append(str) | |
return | |
if (open < max): | |
backtrack(list, str + "(", open + 1, close, max) | |
if (close < open): | |
backtrack(list, str + ")", open, close + 1, max) | |
print(generateParenthesis(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment