I'm using Advent Of Code 2022 to learn Rust. For day 6 we need to find the first group of n characters that are all unique. My approach is to use windows to make HashSets of characters until there is a HashSet whose length is n. Here's what I finally got to work:
fn find_start(signal: &str, window_length: u32) -> Option<u32> {
let chars: Vec<char> = signal.chars().collect();
for (index, window) in chars.windows(window_length as usize).enumerate() {
let char_set: HashSet<_> = HashSet::from_iter(window.iter());
if char_set.len() == window_length as usize {
return Some(index as u32 + window_length);
}
}
None
}
(I know this isn't necessarily the most efficient algorithm but learning Rust is a higher priority than efficiency.)
My questions are:
Is there are more idiomatic way of getting windows of the characters in &str than creating a Vec<char> then calling windows on that Vec?
Is there a cleaner way of making the HashSet from the &[char] than HashSet::from_iter(window.iter())? It seems like a lot of iters.
For text stuff, it usually depends alot on whether you want to solve the problem or learn how to do it properly.
In competitive programming, they essentially never actually make you deal with unicode. So the most expedient way is just to deal in u8s because really when they say "character" in such problems they mean "C's char and only ever ASCII".
If this was real-world input, I'd definitely be Unicode-aware. But this input is constrained and defined so I'm ok with treating it as 7-bit ASCII. Thanks for the link though. I'll keep that in mind when I need to process non-ASCII text.
That's great, thanks. I'm still at the "keep typing until it works" phase so I probably added .iter() before I did HashSet::from_iter() and didn't try removing it.