Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1832.html

1832. Check if the Sentence Is Pangram

Level

Easy

Description

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.

Solution

Use a set to store all the lowercase letters that exist in sentence. Return true if and only if the set’s size is 26.

  • class Solution {
        public boolean checkIfPangram(String sentence) {
            Set<Character> set = new HashSet<Character>();
            int length = sentence.length();
            for (int i = 0; i < length; i++) {
                char c = sentence.charAt(i);
                if (c >= 'a' && c <= 'z')
                    set.add(c);
            }
            return set.size() == 26;
        }
    }
    
    ############
    
    class Solution {
        public boolean checkIfPangram(String sentence) {
            int mask = 0;
            for (int i = 0; i < sentence.length(); ++i) {
                mask |= 1 << (sentence.charAt(i) - 'a');
            }
            return mask == (1 << 26) - 1;
        }
    }
    
  • // OJ: https://leetcode.com/problems/check-if-the-sentence-is-pangram/
    // Time: O(N)
    // Space: O(C) where C is the length of the character set
    class Solution {
    public:
        bool checkIfPangram(string s) {
            unordered_set<char> ss(begin(s), end(s));
            return ss.size() == 26;
        }
    };
    
  • class Solution:
        def checkIfPangram(self, sentence: str) -> bool:
            return len(set(sentence)) == 26
    
    ############
    
    # 1832. Check if the Sentence Is Pangram
    # https://leetcode.com/problems/check-if-the-sentence-is-pangram/
    
    class Solution:
        def checkIfPangram(self, sentence: str) -> bool:
            c = collections.Counter(sentence)
    
            return len(c) >= 26
    
    
  • func checkIfPangram(sentence string) bool {
    	mask := 0
    	for _, c := range sentence {
    		mask |= 1 << int(c-'a')
    	}
    	return mask == 1<<26-1
    }
    
  • function checkIfPangram(sentence: string): boolean {
        let mark = 0;
        for (const c of sentence) {
            mark |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
        }
        return mark === (1 << 26) - 1;
    }
    
    
  • impl Solution {
        pub fn check_if_pangram(sentence: String) -> bool {
            let mut mark = 0;
            for c in sentence.as_bytes() {
                mark |= 1 << *c - b'a';
            }
            mark == (1 << 26) - 1
        }
    }
    
    

All Problems

All Solutions