-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-parentheses.rs
45 lines (41 loc) · 1.09 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
struct Solution;
impl Solution {
pub fn is_valid(s: String) -> bool {
let mut ans: Vec<char> = vec![' '];
for i in s.chars() {
match i {
'(' | '[' | '{' => ans.push(i),
')' => {
if ans.pop().unwrap() != '(' {
return false;
}
}
']' => {
if ans.pop().unwrap() != '[' {
return false;
}
}
'}' => {
if ans.pop().unwrap() != '{' {
return false;
}
}
_ => return false,
}
}
ans.len() == 1
}
}
fn main() {
println!("{}", Solution::is_valid("{[()]}".to_string()));
println!("{}", Solution::is_valid("]{[()]}".to_string()));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert!(Solution::is_valid("{[()]}".to_string()));
assert!(!Solution::is_valid("]{[()]}".to_string()));
}
}