-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20-valid-parentheses.cpp
40 lines (36 loc) · 1.5 KB
/
20-valid-parentheses.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Title: Valid Parentheses
// Description:
// 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.
// Link: https://leetcode.com/problems/valid-parentheses/
// Time complexity: O(n)
// Space complexity: O(n)
class Solution {
public:
bool isValid(string s) {
// use a dummy head so we don't need to check if the stack is empty :D!
std::stack<char> parens({ '\0' });
// process each paren
for (char c: s) {
if (c == '(' || c == '{' || c == '[') {
// put the opening paren into the stack
parens.push(c);
} else /* if (c == ')' || c == '}' || c == ']') */ {
// the closing paren should match the previous opening paren
if (c == ')') {
if (parens.top() != '(') return false;
} else if (c == '}') {
if (parens.top() != '{') return false;
} else if (c == ']') {
if (parens.top() != '[') return false;
}
// remove the matched opening paren
parens.pop();
}
}
// check if there is no remaining parens (only contains the dummy head)
return parens.top() == '\0';
}
};