Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

Solution

func generateParenthesis(n int) []string {
  result := []string{}
  current := []byte{}
 
  helper(n,0,0, &current, &result);

  return result
}

func helper(n,left, right int, current *[]byte, result *[]string) {
    fmt.Println(string(*current))
  if left == n && right == n {
      *result = append(*result, string(*current))
      return
  }
    
    if(left < n) {
      *current = append(*current,'(') 
       helper(n, left+1, right, current, result)
        *current = (*current)[:len(*current)-1] //delete last char
    }

    if left > right {
        *current = append(*current,')') 
        helper(n, left, right+1, current, result)
        *current = (*current)[:len(*current)-1]
    }
}