Parentheses Problem:
Generate all valid combination of n pairs of parentheses.
Algorithm:
- If opening bracket and closing brackets equal to total parentheses then
- Add characters array to parentheses list
- Else
- If opening brackets less than number of pairs then
- Add ‘(‘ to character array
- recursively call with opening bracket + 1
- If closing brackets less than number of pairs then
- Add ‘)’ to character array
- recursively call the closing bracket + 1
- If opening brackets less than number of pairs then
Latest Source Code:
Github: ParenthesesProblem.java
Output:
Parentheses (1): [()] Parentheses (2): [(()), ()()] Parentheses (3): [((())), (()()), (())(), ()(()), ()()()] Parentheses (4): [(((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()]