Created
January 1, 2019 05:04
-
-
Save ttsugriy/3dc568c9e55b90233849090707f065b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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