Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Added code for GenerateParentheses in GoLang #545

Merged
merged 12 commits into from
Oct 14, 2021
53 changes: 53 additions & 0 deletions Programming/Golang/GenerateParentheses/GenerateParentheses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

//Steps: 1. Add left paren only when open < n and right paren only when close < open

package main

import "fmt"

func main() {
// Taking Example Input- 1
output := generateParenthesis(3)
fmt.Println(output)

// Taking Example Input-2
output = generateParenthesis(1)
fmt.Println(output)

}

//Priting the pairs
func generateParenthesis(n int) []string {
input := ""
for i := 0; i < n; i++ {
input = input + " "
}
output := generateParenthesisUtil(0, n, 0, 0, []rune(input))
return output
}

//Recursive Function
func generateParenthesisUtil(pos, n, open, close int, input []rune) []string {

var output []string
if pos == n*2 {
output = append(output, string(input))
return output
}

if close < open {
input[pos] = ')'
result := generateParenthesisUtil(pos+1, n, open, close+1, input)
output = append(output, result...)

}

if open < n {
input[pos] = '('
result := generateParenthesisUtil(pos+1, n, open+1, close, input)
output = append(output, result...)
}

return output
}