-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWarmup_Valid Parentheses_20.cpp
More file actions
67 lines (58 loc) · 2.29 KB
/
Warmup_Valid Parentheses_20.cpp
File metadata and controls
67 lines (58 loc) · 2.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
UCLA ACM ICPC: Interview Track Leetcode Problem Solutions
Disclaimer: This is not the only valid solution and we do not claim our solutions
to be optimal in terms of runtime or memory usage, we are merely giving example
solutions to questions for learning purposes only
Valid Parentheses
Leetcode Problem 20
https://leetcode.com/problems/valid-parentheses/
*/
class Solution {
public:
// The strategy for this problem is to use a character stack.
// We traverse the string from left to right, putting open
// parenthesis into the stack, and attemping to match closing
// parenthesis with the top of the stack.
bool isValid(string s) {
stack<char> stack;
for(int i = 0; i < s.size(); i++) {
char cur = s[i];
// If the current character is an open parenthesis,
// push it onto the stack
if(cur == '(' || cur == '{' || cur == '[') {
stack.push(cur);
}
// If the current character is ')', first check
// if the stack is empty. If it is, then ')' can't
// be matched with anything. Next, check if the
// top of the stack is '('. It it isn't, then ')'
// is matched with the wrong open parenthesis.
// If it is, then we have a match, and we can
// remove the open parenthesis from the stack.
else if(cur == ')') {
if(stack.empty() || stack.top() != '(')
return false;
else
stack.pop();
}
// Follow a similar process for '}'
else if(cur == '}') {
if(stack.empty() || stack.top() != '{')
return false;
else
stack.pop();
}
// Follow a similar process for ']'
else if(cur == ']') {
if(stack.empty() || stack.top() != '[')
return false;
else
stack.pop();
}
}
// Finally, we must check if the stack is empty at the end.
// It is wasn't then we would have extra open parenthesis
// that aren't matched with any closing parenthesis.
return stack.empty();
}
};