Skip to content

Commit

Permalink
1832. Check if the Sentence Is Pangram: AC
Browse files Browse the repository at this point in the history
  • Loading branch information
tan-wei committed Jan 5, 2025
1 parent 2218c81 commit aea0cb1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/solution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
69 changes: 69 additions & 0 deletions src/solution/s1832_check_if_the_sentence_is_pangram.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit aea0cb1

Please sign in to comment.