Skip to content

Commit

Permalink
Refactored anagram, alive_halves and bitset solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Jul 22, 2024
1 parent a96af81 commit 9f02f4d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
11 changes: 3 additions & 8 deletions src/leetcode/alike_halves.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
36 changes: 16 additions & 20 deletions src/leetcode/bitset.rs
Original file line number Diff line number Diff line change
@@ -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<RefCell<HashSet<i32>>>,
zeros: Rc<RefCell<HashSet<i32>>>,
ones: HashSet<i32>,
zeros: HashSet<i32>,
}

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::<String>();
.collect();
write!(f, "{}", result)
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/other/anagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 9f02f4d

Please sign in to comment.