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:
[ ”((()))”, ”(()())”, ”(())()”, ”()(())”, ”()()()” ]
func generateParenthesis(n int) []string {
if n==0{
return make([]string,0)
}
ret:=make([]string,0)
if n==1{
ret=append(ret,"()")
return ret
}
tmp:=generateParenthesis(n-1)
for i:=0;i<len(tmp);i++{
ret=insert(ret,"("+tmp[i]+")")
ret=insert(ret,"()"+tmp[i])
ret=insert(ret,tmp[i]+"()")
for j:=0;j<len(tmp[i]);j++{
if string(tmp[i][j])=="("&&j+1<len(tmp[i]){
netret:=tmp[i][0:j+1]+"()"+tmp[i][j+1:]
ret=insert(ret,netret)
}
}
}
return ret
}
func insert(strs[]string,str string)[]string{
if !contains(strs,str){
strs=append(strs,str)
}
return strs
}
func contains(strs[]string,str string)bool{
for i:=0;i<len(strs);i++{
if strs[i]==str{
return true
}
}
return false
}