2 ms·
I ran the unoptimized version of the code into ChatGPT and he was able to optimize it similarly to OPs solutions. I was quite impressed, here is the ChatGPT ve
by nerder92 4y ago
I ran the unoptimized version of the code into ChatGPT and he was able to optimize it similarly to OPs solutions.
I was quite impressed, here is the ChatGPT version:
fn run(s: &[char], window_size: usize) -> usize {
let mut unique_chars = 0;
for i in 0..window_size {
unique_chars |= 1 << (s[i] as u32 - 'a' as u32);
}
if unique_chars.count_ones() as usize == window_size {
return window_size;
}
for i in 1..s.len() - window_size {
let prev = s[i - 1] as u32 - 'a' as u32;
let next = s[i + window_size - 1] as u32 - 'a' as u32;
unique_chars ^= 1 << prev;
unique_chars |= 1 << next;
if unique_chars.count_ones() as usize == window_size {
return i + window_size;
}
}
panic!("No unique window found");
}
//NOTE: my prompt was the make the code O(N)