Golang 0ms with map and stack


  • 0
    R
    func isValid(s string) bool {
        parenth_dict := make(map[rune]rune)
        
        parenth_dict['('] = ')'
        parenth_dict['{'] = '}'
        parenth_dict['['] = ']'
        
        aux := make([]rune, 0)
        
        for i:= 0; i < len(s); i++ {
           if s[i] == '{' || s[i] == '(' || s[i] == '[' {
               aux = append(aux, rune(s[i]))
           } else if len(aux) == 0 || parenth_dict[aux[len(aux) - 1]] != rune(s[i]) {
               return false;
           } else {
               aux = aux[:len(aux) - 1]
           }
        }
        return len(aux) == 0
    }
    

Log in to reply
 

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