678.valid-parenthesis-string.go 523 B

1234567891011121314151617181920212223242526272829
  1. func checkValidString(s string) bool {
  2. set := make(map[int]bool) // Records ALL POSSIBLE VALID num of lb - rb
  3. set[0] = true
  4. for _, r := range s {
  5. nset := make(map[int]bool)
  6. switch r {
  7. case '(':
  8. for k := range set {
  9. nset[k+1] = true
  10. }
  11. case ')':
  12. for k := range set {
  13. if 0 < k {
  14. nset[k-1] = true
  15. }
  16. }
  17. default:
  18. for k := range set {
  19. nset[k+1] = true
  20. nset[k] = true
  21. if 0 < k {
  22. nset[k-1] = true
  23. }
  24. }
  25. }
  26. set = nset
  27. }
  28. return set[0] // Check if 0 is possible
  29. }