use std::collections::HashSet;
pub fn is_pangram(sentence: &str) -> bool {
sentence
.to_lowercase()
.chars()
.filter(|c| c.is_ascii_alphabetic())
.collect::<HashSet<char>>()
.len()
== 26
}
This approach chains several functions together to determine the result.
- It first passes the
sentence
to_lowercase. - The lowercased
sentence
is then iterated by chars. - The
chars
are filtered in its closure so that only a character that is_ascii_alphabetic makes it through to be collected into a HashSet. - The function returns if the len of the
HashSet
is26
. If the number of unique letters in theHashSet
is equal to the26
letters in the alphabet, then the function will returntrue
.
11th Dec 2024
·
Found it useful?