Advertisement
lazar955

Untitled

Oct 24th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.50 KB | None | 0 0
  1. func isValid(s string) bool {
  2.     if len(s) == 0 || len(s) % 2 == 1 {
  3.         return false
  4.     }
  5.  
  6.     pairs := map[rune]rune {
  7.         '(':')',
  8.         '{':'}',
  9.         '[':']',
  10.     }
  11.     stack:=[]rune{}
  12.  
  13.     for _,r:= range s {
  14.         if _, ok:=pairs[r]; ok {
  15.             stack = append(stack,r)
  16.         }else if len(stack) == 0 || pairs[stack[len(stack)-1]] != r {
  17.             return false
  18.         }else {
  19.             stack = stack[:len(stack)-1]
  20.         }
  21.     }
  22.     return len(stack) == 0
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement