Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
func isValid(s string) bool {
sig:=make([]string,0)
for i:=0;i<len(s);i++{
if left(string(s[i])){
sig=append(sig,string(s[i]))
}
if right(string(s[i])){
if len(sig)>0{
if bingo(sig[len(sig)-1], string(s[i])){
sig=sig[0:len(sig)-1]
}else if right(sig[len(sig)-1]){
sig=append(sig,string(s[i]))
}else {
return false
}
}else{
return false
}
}
}
if len(sig)==0{
return true
}else{
return false
}
}
func right(str string)bool{
if str==")"|| str=="}"|| str=="]"{
return true
}else {
return false
}
}
func left(str string)bool{
if str=="("|| str=="{"|| str=="["{
return true
}else{
return false
}
}
func bingo(left string,right string)bool{
if left=="("&&right==")"{
return true
}
if left=="{"&&right=="}"{
return true
}
if left=="["&&right=="]"{
return true
}
return false
}