Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Example 1:
1 | Input: s = "()" |
Example 2:
1 | Input: s = "()[]{}" |
Example 3:
1 | Input: s = "(]" |
Example 4:
1 | Input: s = "([)]" |
Example 5:
1 | Input: s = "{[]}" |
定義符號對,遇到符號押入,配對成功移出,檢查陣列是否空。
1 | Time: O(n), Space: O(n) |
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)