|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: easy |
| 4 | +# Follow `Topics` tags |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Stack |
| 8 | +--- |
| 9 | + |
| 10 | +# [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +You are given a string s that consists only of the characters `(`, `)`, `{`, `}`, `[`, and `]`. Your task is to determine whether the string is valid. |
| 15 | + |
| 16 | +A string is considered valid if it meets the following conditions: |
| 17 | + |
| 18 | +1. Every opening bracket has a matching closing bracket of the same type. |
| 19 | +2. Brackets must close in the correct order (i.e., the most recently opened bracket must be the first one to close). |
| 20 | +3. There should not be any unmatched brackets. |
| 21 | + |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | +``` |
| 25 | +Input: s = "{}" |
| 26 | +Output: true |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | +``` |
| 31 | +Input: s = "{]" |
| 32 | +Output: false |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +**Constraints:** |
| 37 | +`1 <= s.length <= 104` |
| 38 | +`s` consists of parentheses only `'()[]{}'`. |
| 39 | + |
| 40 | +## Solution |
| 41 | + |
| 42 | +As known valid combination: |
| 43 | +``` |
| 44 | +mapping = { ')': '(', '}': '{', ']': '[' } |
| 45 | +``` |
| 46 | + |
| 47 | +Let stack has only open parentheses. When a close parenthesis comes, use it as a key to get valid open parenthesis in the mapping. If mapped parentheses and stack top parenthese sare not valid combination, we should return False. |
| 48 | + |
| 49 | +At last, if stack is empty, we should return True. |
| 50 | + |
| 51 | +```java |
| 52 | +class Solution { |
| 53 | + public boolean isValid(String s) { |
| 54 | + Stack<Character> stack = new Stack<>(); |
| 55 | + for (char c : s.toCharArray()) { |
| 56 | + if (c == '(' || c == '{' || c == '[') { |
| 57 | + stack.push(c); |
| 58 | + } else { |
| 59 | + if (stack.isEmpty()) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + char top = stack.pop(); |
| 63 | + if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '[')) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return stack.isEmpty(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +```python |
| 74 | +class Solution: |
| 75 | + def isValid(self, s: str) -> bool: |
| 76 | + stack = [] |
| 77 | + mapping = {')': '(', '}': '{', ']': '['} |
| 78 | + for char in s: |
| 79 | + if char in mapping.values(): |
| 80 | + stack.append(char) |
| 81 | + elif char in mapping: |
| 82 | + if stack == [] or mapping[char] != stack.pop(): |
| 83 | + return False |
| 84 | + return not stack |
| 85 | +``` |
| 86 | + |
| 87 | +## Complexity |
| 88 | + |
| 89 | +- Time complexity: $$O(n)$$ |
| 90 | +<!-- Add time complexity here, e.g. $$O(n)$$ --> |
| 91 | + |
| 92 | +- Space complexity: $$O(n)$$ |
| 93 | +<!-- Add space complexity here, e.g. $$O(n)$$ --> |
0 commit comments