Stack¶
Table of Contents¶
- 20. Valid Parentheses (Easy)
20. Valid Parentheses¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string, stack
- Determine if the input string is valid.
- Steps for the string
()[]{}
:
char | action | stack |
---|---|---|
( |
push | "(" |
) |
pop | "" |
[ |
push | "[" |
] |
pop | "" |
{ |
push | "{" |
} |
pop | "" |
20. Valid Parentheses - Python Solution
# Stack
def isValid(s: str) -> bool:
hashmap = {
")": "(",
"]": "[",
"}": "{",
}
stack = []
for c in s:
if c in hashmap:
if stack and stack[-1] == hashmap[c]:
stack.pop()
else:
return False
else:
stack.append(c)
return True if not stack else False
print(isValid("()")) # True
print(isValid("()[]{}")) # True
print(isValid("(]")) # False
20. Valid Parentheses - C++ Solution
#include <cassert>
#include <stack>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool isValid(string s) {
unordered_map<char, char> map{{')', '('}, {'}', '{'}, {']', '['}};
stack<char> stack;
if (s.length() % 2 == 1) return false;
for (char& ch : s) {
if (stack.empty() || map.find(ch) == map.end()) {
stack.push(ch);
} else {
if (map[ch] != stack.top()) {
return false;
}
stack.pop();
}
}
return stack.empty();
}
};
int main() {
Solution s;
assert(s.isValid("()") == true);
assert(s.isValid("()[]{}") == true);
assert(s.isValid("(]") == false);
assert(s.isValid("([)]") == false);
assert(s.isValid("{[]}") == true);
return 0;
}