valid-parentheses

This commit is contained in:
Gleb Koval 2022-06-06 19:51:18 +01:00
parent 582b512e28
commit f8f910b606
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 21 additions and 0 deletions

21
valid-parentheses/sol.rs Normal file
View 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
}
}