When concerning ordering, stack is a good data structure to use.
Solution:
# T:O(n) S:O(1) class Solution: # @param {string} s # @return {boolean} def isValid(self, s): dict, stk, N = {')':'(',']':'[','}':'{'}, [], len(s) for ch in s: if ch in dict.values(): stk.append(ch) else: if not stk or stk.pop() != dict[ch]: return False return not stkRun Time: 48 ms
No comments:
Post a Comment