Simple Go solution using slice


  • 0
    S
    func isValid(s string) bool {
    	var st []rune
    
    	for _, e := range s {
    		if e == '(' || e == '{' || e == '[' {
    			st = append(st, e)
    		} else if e == '}' || e == ')' || e == ']' {
    			if len(st) < 1 {
    				return false
    			}
    			top := st[len(st)-1]
    			if top == '(' && e != ')' || 
                               top == '{' && e != '}' || 
                               top == '[' && e != ']' {
    				return false
    			}
    			st = st[:len(st)-1]
    		}
    	}
    	return len(st) == 0
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.