valid-parentheses
This commit is contained in:
21
valid-parentheses/sol.rs
Normal file
21
valid-parentheses/sol.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
impl Solution {
|
||||
pub fn is_valid(s: String) -> bool {
|
||||
let mut stack = vec![];
|
||||
s.chars()
|
||||
.all(|chr| match chr {
|
||||
'(' | '{' | '[' => {
|
||||
stack.push(chr);
|
||||
true
|
||||
},
|
||||
_ => {
|
||||
let opened = stack.pop()
|
||||
.unwrap_or(' ');
|
||||
// Hacky way to check if the brackets match. Not scalable, but works here.
|
||||
if chr as u8 / 10 != opened as u8 / 10 {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
}) && stack.len() == 0
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user