From 9f02f4d0de1345297da92f0c38cb58fbb0459361 Mon Sep 17 00:00:00 2001 From: jusexton Date: Mon, 22 Jul 2024 02:06:36 -0500 Subject: [PATCH] Refactored anagram, alive_halves and bitset solutions. --- src/leetcode/alike_halves.rs | 11 +++-------- src/leetcode/bitset.rs | 36 ++++++++++++++++-------------------- src/other/anagram.rs | 7 ++----- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/leetcode/alike_halves.rs b/src/leetcode/alike_halves.rs index efa91a9..3d79db2 100644 --- a/src/leetcode/alike_halves.rs +++ b/src/leetcode/alike_halves.rs @@ -1,16 +1,11 @@ -use std::collections::HashSet; +const VOWELS: [char; 10] = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; pub fn halves_are_alike(s: String) -> bool { fn count_vowels(s: &str) -> usize { - let vowels = HashSet::from(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); - s.chars().filter(|c| vowels.contains(c)).count() + s.chars().filter(|c| VOWELS.contains(c)).count() } - let middle = s.len() / 2; - let first = &s.as_str()[0..middle]; - let second = &s.as_str()[middle..]; - - count_vowels(first) == count_vowels(second) + count_vowels(&s[0..middle]) == count_vowels(&s[middle..]) } #[cfg(test)] diff --git a/src/leetcode/bitset.rs b/src/leetcode/bitset.rs index b3d8907..92d67b4 100644 --- a/src/leetcode/bitset.rs +++ b/src/leetcode/bitset.rs @@ -1,62 +1,58 @@ -use std::cell::RefCell; use std::collections::HashSet; use std::fmt::{Display, Formatter}; use std::iter::FromIterator; -use std::rc::Rc; +use std::mem; struct Bitset { size: i32, - ones: Rc>>, - zeros: Rc>>, + ones: HashSet, + zeros: HashSet, } impl Bitset { fn new(size: i32) -> Self { Self { size, - ones: Rc::new(RefCell::new(HashSet::new())), - zeros: Rc::new(RefCell::new(HashSet::from_iter(0..size))), + ones: HashSet::new(), + zeros: HashSet::from_iter(0..size), } } fn fix(&mut self, idx: i32) { - self.ones.borrow_mut().insert(idx); - self.zeros.borrow_mut().remove(&idx); + self.ones.insert(idx); + self.zeros.remove(&idx); } fn unfix(&mut self, idx: i32) { - self.zeros.borrow_mut().insert(idx); - self.ones.borrow_mut().remove(&idx); + self.zeros.insert(idx); + self.ones.remove(&idx); } fn flip(&mut self) { - let temp = self.ones.clone(); - self.ones = self.zeros.clone(); - self.zeros = temp; + mem::swap(&mut self.ones, &mut self.zeros); } fn all(&self) -> bool { - self.ones.borrow().len() == self.size as usize + self.ones.len() == self.size as usize } fn one(&self) -> bool { - self.ones.borrow().len() > 0 + !self.ones.is_empty() } fn count(&self) -> i32 { - self.ones.borrow().len() as i32 + self.ones.len() as i32 } } impl Display for Bitset { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let ones = self.ones.borrow(); - let result = (0..self.size) - .map(|idx| match ones.contains(&idx) { + let result: String = (0..self.size) + .map(|idx| match self.ones.contains(&idx) { true => '1', false => '0', }) - .collect::(); + .collect(); write!(f, "{}", result) } } diff --git a/src/other/anagram.rs b/src/other/anagram.rs index 131b244..e7e49b1 100644 --- a/src/other/anagram.rs +++ b/src/other/anagram.rs @@ -7,17 +7,14 @@ fn are_anagrams(a: &str, b: &str) -> bool { if a.len() != b.len() { return false; } - - let a_product = str_to_prime_product(a); - let b_product = str_to_prime_product(b); - a_product == b_product + str_to_prime_product(a) == str_to_prime_product(b) } fn str_to_prime_product(input: &str) -> u64 { input .to_ascii_lowercase() .chars() - .map(|c| (c as u8) - b'a') + .map(|c| c as u8 - b'a') .map(|idx| PRIMES[idx as usize]) .product() }