-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
56 lines (46 loc) · 1.42 KB
/
main.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
fn is_valid(s: String) -> bool {
// Create a stack to store the parentheses
let mut stack: Vec<char> = Vec::with_capacity(s.len());
// Iterate over the input string while checking the each character
let check_stack = |stack: &mut Vec<char>, expect: char| stack.pop().unwrap_or('0') == expect;
for parentheses in s.chars() {
let status: bool = match parentheses {
// Push opening parentheses into the stack
'(' | '[' | '{' => {
stack.push(parentheses);
true
},
// Check that closing parentheses are after their opening counterpart
')' => check_stack(&mut stack, '('),
']' => check_stack(&mut stack, '['),
'}' => check_stack(&mut stack, '{'),
_ => false,
};
if !status {
return false;
}
}
stack.is_empty()
}
#[cfg(test)]
mod tests {
use crate::is_valid;
fn test_input(input: String, expect: bool) -> bool {
is_valid(input) == expect
}
#[test]
fn parentheses_only() {
assert!(test_input(String::from("()"), true))
}
#[test]
fn all_parentheses() {
assert!(test_input(String::from("()[]{}"), true))
}
#[test]
fn invalid_closing_parentheses() {
assert!(test_input(String::from("(]"), false))
}
}
fn main() {
println!("{:?}", is_valid(String::from("()[]{}")));
}