Skip to content

Latest commit

 

History

History
135 lines (105 loc) · 2.78 KB

File metadata and controls

135 lines (105 loc) · 2.78 KB

中文文档

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.

Solutions

Python3

Set:

class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        return len(set(sentence)) == 26

Bit Manipulation:

class Solution:
    def checkIfPangram(self, sentence: str) -> bool:
        res = 0
        for c in sentence:
            res |= (1 << (ord(c) - ord('a')))
            if res == 0x3ffffff:
                return True
        return False

Java

HashSet:

class Solution {
    public boolean checkIfPangram(String sentence) {
        Set<Character> s = new HashSet<>();
        for (char c : sentence.toCharArray()) {
            s.add(c);
            if (s.size() == 26) {
                return true;
            }
        }
        return false;
    }
}

Bit Manipulation:

class Solution {
    public boolean checkIfPangram(String sentence) {
        int res = 0;
        for (char c : sentence.toCharArray()) {
            res |= (1 << (c - 'a'));
            if (res == 0x3ffffff) {
                return true;
            }
        }
        return false;
    }
}

C++

class Solution {
public:
    bool checkIfPangram(string sentence) {
        int res = 0;
        for (char c : sentence) {
            res |= (1 << (c - 'a'));
            if (res == 0x3ffffff) return true;
        }
        return false;
    }
};

Go

func checkIfPangram(sentence string) bool {
	res := 0
	for _, c := range sentence {
		res |= (1 << (c - 'a'))
		if res == 0x3ffffff {
			return true
		}
	}
	return false
}

...