-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalid-parentheses.rs
103 lines (99 loc) · 3.53 KB
/
valid-parentheses.rs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 20. Valid Parentheses
// 🟢 Easy
//
// https://leetcode.com/problems/valid-parentheses/
//
// Tags: String - Stack
use std::collections::HashMap;
struct Solution;
impl Solution {
/// Use a stack to push opening symbols that we see. Iterate over the input,
/// if we see an opening symbol, push it into the stack, if we see a
/// closing symbol, check that it matches the top of the stack, if it does,
/// pop that symbol, if it doesn't return false. Once we run out of symbols,
/// return whether the stack is empty.
///
/// Time complexity: O(n) - We visit all characters in the input string and
/// do O(1) work for each.
/// Space complexity: O(n) - The stack grows in size linearly with the input.
///
/// Runtime 1 ms Beats 75.94%
/// Memory 2 MB Beats 99.77%
pub fn is_valid(s: String) -> bool {
let mut stack = vec![];
for c in s.chars() {
match c {
'{' | '(' | '[' => stack.push(c),
'}' => {
if stack.is_empty() || stack.pop().unwrap() != '{' {
return false;
}
}
')' => {
if stack.is_empty() || stack.pop().unwrap() != '(' {
return false;
}
}
']' => {
if stack.is_empty() || stack.pop().unwrap() != '[' {
return false;
}
}
_ => return false,
}
}
stack.is_empty()
}
/// Similar to the previous solution but use a hashmap to make comparing
/// symbols cleaner, it also makes the code more maintainable, if we want
/// to match more symbols, we just need to add them to the hashmap.
/// Use a stack to push opening symbols that we see. Iterate over the input,
/// if we see an opening symbol, push it into the stack, if we see a
/// closing symbol, check that it matches the top of the stack, if it does,
/// pop that symbol, if it doesn't return false. Once we run out of symbols,
/// return whether the stack is empty.
///
/// Time complexity: O(n) - We visit all characters in the input string and
/// do O(1) work for each.
/// Space complexity: O(n) - The stack grows in size linearly with the input.
///
/// Runtime 0 ms Beats 100%
/// Memory 2.1 MB Beats 81.98%
pub fn is_valid_2(s: String) -> bool {
let mut stack = vec![];
let symbols = HashMap::from([(')', '('), ('}', '{'), (']', '[')]);
for c in s.chars() {
match c {
'{' | '(' | '[' => stack.push(c),
'}' | ')' | ']' => match stack.pop() {
Some(o) => match symbols.get(&c) {
Some(v) => {
if *v != o {
return false;
}
}
None => return false,
},
None => return false,
},
_ => return false,
}
}
stack.is_empty()
}
}
// Tests.
fn main() {
let tests = [
("(", false),
("()", true),
("(]", false),
("{[]}", true),
("()[]{}", true),
];
for t in tests {
assert_eq!(Solution::is_valid(String::from(t.0)), t.1);
assert_eq!(Solution::is_valid_2(String::from(t.0)), t.1);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}