Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isValid(self, s: str) -> bool:
- newlist = []
- s = [str(i) for i in s]
- for i in range(len(s)):
- if "[" in s:
- if "]" in s:
- s.remove("[")
- s.remove("]")
- else:
- newlist.append("[")
- elif "(" in s:
- if ")" in s:
- s.remove("(")
- s.remove(")")
- else:
- newlist.append("(")
- elif "{" in s:
- if "}" in s:
- s.remove("{")
- s.remove("}")
- else:
- newlist.append("{")
- else:
- newlist.append(s[i])
- return (bool(len(newlist) == 0))
- '''
- DNF
- Solution:
- bracket_map = {"(": ")", "[": "]", "{": "}"}
- open_par = set(["(", "[", "{"])
- stack = []
- for i in s:
- if i in open_par:
- stack.append(i)
- elif stack and i == bracket_map[stack[-1]]:
- stack.pop()
- else:
- return False
- return stack == []
- Notes:
- I was too complex with my attempted solution. The solution about puts everything in a map, and then checks that map against the string. Need to be able to think more simply and not code for a step-by-step basis.
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement