Parentheses Problem

Parentheses Problem:

Generate all valid combination of n pairs of parentheses.
Algorithm:
  1. If opening bracket and closing brackets equal to total parentheses then
    1. Add characters array to parentheses list
  2. Else
    1. If opening brackets less than number of pairs then
      1. Add ‘(‘ to character array
      2. recursively call with opening bracket + 1
    2. If closing brackets less than number of pairs then
      1. Add ‘)’ to character array
      2. recursively call the closing bracket + 1

Latest Source Code:
Github: ParenthesesProblem.java


Output:

 Parentheses (1): [()]
Parentheses (2): [(()), ()()]
Parentheses (3): [((())), (()()), (())(), ()(()), ()()()]
Parentheses (4): [(((()))), ((()())), ((())()), ((()))(), (()(())), (()()()), (()())(), (())(()), (())()(), ()((())), ()(()()), ()(())(), ()()(()), ()()()()]

Video

Author: Hrishikesh Mishra