Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created January 1, 2019 05:04
Show Gist options
  • Save ttsugriy/3dc568c9e55b90233849090707f065b3 to your computer and use it in GitHub Desktop.
Save ttsugriy/3dc568c9e55b90233849090707f065b3 to your computer and use it in GitHub Desktop.
impl Solution {
pub fn is_valid(s: String) -> bool {
let mut stack = Vec::new();
let match_of = |c: char| -> char {
match c {
')' => '(',
'}' => '{',
']' => '[',
_ => panic!("Invalid closing bracket"),
}
};
for ch in s.chars() {
match ch {
'(' | '{' | '[' => stack.push(ch),
closing => {
if let Some(closing_bracket) = stack.pop() {
if closing_bracket != match_of(closing) {
return false;
}
} else {
return false;
}
}
}
}
stack.is_empty()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment