Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Concise solution in Go

fellainthewagon

Jul 6, 2024

type Solution struct{} func (s *Solution) isValid(str string) bool { par := map[byte]byte{ '[': ']', '(': ')', '{': '}', } stack := []byte{} for i := 0; i < len(str); i++ { curr := str[i] if v, ok := par[curr]; ok { // if curr is opening push its closing to stack stack = append(stack, v) } else if len(stack) != 0 && curr == stack[len(stack)-1] { // otherwise curr should be correct closing - so pop it from stack stack = stack[:len(stack)-1] } else { // in that case curr is incorrect closing return false } } return len(stack) == 0 }

0

0

Comments
Comments

On this page