diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 22401d32..5809008c 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -1380,3 +1380,4 @@ mod s1827_minimum_operations_to_make_the_array_increasing; mod s1828_queries_on_number_of_points_inside_a_circle; mod s1829_maximum_xor_for_each_query; mod s1830_minimum_number_of_operations_to_make_string_sorted; +mod s1832_check_if_the_sentence_is_pangram; diff --git a/src/solution/s1832_check_if_the_sentence_is_pangram.rs b/src/solution/s1832_check_if_the_sentence_is_pangram.rs new file mode 100644 index 00000000..edbf3d43 --- /dev/null +++ b/src/solution/s1832_check_if_the_sentence_is_pangram.rs @@ -0,0 +1,69 @@ +/** + * [1832] Check if the Sentence Is Pangram + * + * A pangram is a sentence where every letter of the English alphabet appears at least once. + * Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise. + * + * Example 1: + * + * Input: sentence = "thequickbrownfoxjumpsoverthelazydog" + * Output: true + * Explanation: sentence contains at least one of every letter of the English alphabet. + * + * Example 2: + * + * Input: sentence = "leetcode" + * Output: false + * + * + * Constraints: + * + * 1 <= sentence.length <= 1000 + * sentence consists of lowercase English letters. + * + */ +pub struct Solution {} + +// problem: https://leetcode.com/problems/check-if-the-sentence-is-pangram/ +// discuss: https://leetcode.com/problems/check-if-the-sentence-is-pangram/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +const N_LETTERS: usize = (b'z' - b'a' + 1) as _; + +impl Solution { + pub fn check_if_pangram(sentence: String) -> bool { + sentence + .bytes() + .scan(0_u32, |bitset, b| { + *bitset |= 1 << (b - b'a'); + Some(*bitset) + }) + .any(|bitset| bitset == (1 << N_LETTERS) - 1) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1832_example_1() { + let sentence = "thequickbrownfoxjumpsoverthelazydog".to_string(); + + let result = true; + + assert_eq!(Solution::check_if_pangram(sentence), result); + } + + #[test] + fn test_1832_example_2() { + let sentence = "leetcode".to_string(); + + let result = false; + + assert_eq!(Solution::check_if_pangram(sentence), result); + } +}