Performance issue about the Isogram problem iteration approach

This approach will consume all items in the iterator. But once we find a duplicate letter, we can stop early and sure that the word isn’t an Isogram.

Instead, we can do it like the following, because of the laziness evaluation of iterator.

pub fn check(candidate: &str) -> bool {
    let mut seen = HashSet::<char>::new();
    candidate
        .chars()
        .filter(|&c| c.is_ascii_alphabetic())
        .filter(|&c| !seen.insert(c.to_ascii_lowercase()))
        .next()
        == None
}

Most of the community solutions don’t realized this potential problem when the inputs are huge.

Yup! Ideally that’s the sort of thing a mentor would mention during a code review and is just one reason a code review can be so beneficial! Additionally, the Dig Deeper docs, as they get built out, should contain helpful tidbits like this. Speaking of which, if it’s not yet there, this would make a great contribution to the Approaches docs! How do you feel about contributing to the track docs? Disregard

Iterator::all is short-circuiting

1 Like

:joy: Yes, it’s short-circuting. My first thought when I saw the all keyword is wrong. It’s anti-intuition in some degrees.