From c4989cf1180c2838a15fbf1d69b1206e5941ea0e Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Sat, 17 Jul 2021 21:44:05 -0500 Subject: [PATCH] Only 3 pairs, move away from HashSet --- src/lib.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c818158..802b59e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,14 @@ pub fn brackets_are_balanced(string: &str) -> bool { - use std::collections::HashMap; - - let pairs: HashMap = [(')', '('), ('}', '{'), (']', '[')] - .iter().cloned().collect(); let mut stack = vec![]; for char in string.chars() { match char { '(' | '{' | '[' => stack.push(char), - ')' | '}' | ']' => { - if let Some(left) = stack.pop() { - if let Some(right) = pairs.get(&char) { - if left != *right { return false } - } - } else { return false } - }, - _ => {}, + ')' => if stack.pop() != Some('(') { return false }, + '}' => if stack.pop() != Some('{') { return false }, + ']' => if stack.pop() != Some('[') { return false }, + _ => {}, } } stack.is_empty()