具体的,我们遍历 sentence 中的每个字符 c,如果 c 是字母表中的第 i~(0 \le i \lt 26) 个字母,就将 exist}[i] 置为 true。最后检查 exist 中是否存在 false,如果存在返回 false,否则返回 true。
代码
[sol1-Python3]
1 2 3 4 5 6
classSolution: defcheckIfPangram(self, sentence: str) -> bool: exist = [False] * 26 for c in sentence: exist[ord(c) - ord('a')] = True returnall(exist)
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: boolcheckIfPangram(string sentence){ vector<int> exist(26); for (auto c : sentence) { exist[c - 'a'] = true; } for (auto x : exist) { if (x == 0) { returnfalse; } } returntrue; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { publicbooleancheckIfPangram(String sentence) { boolean[] exist = newboolean[26]; for (inti=0; i < sentence.length(); i++) { charc= sentence.charAt(i); exist[c - 'a'] = true; } for (boolean x : exist) { if (!x) { returnfalse; } } returntrue; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassSolution { publicboolCheckIfPangram(string sentence) { bool[] exist = newbool[26]; foreach (char c in sentence) { exist[c - 'a'] = true; } foreach (bool x in exist) { if (!x) { returnfalse; } } returntrue; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funccheckIfPangram(sentence string)bool { exist := [26]bool{} for _, c := range sentence { exist[c-'a'] = true } for _, b := range exist { if !b { returnfalse } } returntrue }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var checkIfPangram = function(sentence) { const exist = newArray(26).fill(0); for (let i = 0; i < sentence.length; i++) { const c = sentence[i]; exist[c.charCodeAt() - 'a'.charCodeAt()] = true; } for (const x of exist) { if (!x) { returnfalse; } } returntrue; };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
boolcheckIfPangram(char * sentence) { int exist[26]; memset(exist, 0, sizeof(exist)); for (int i = 0; sentence[i] != '\0'; i++) { exist[sentence[i] - 'a'] = 1; } for (int i = 0; i < 26; i++) { if (exist[i] == 0) { returnfalse; } } returntrue; }
classSolution: defcheckIfPangram(self, sentence: str) -> bool: state = 0 for c in sentence: state |= 1 << (ord(c) - ord('a')) return state == (1 << 26) - 1
[sol2-C++]
1 2 3 4 5 6 7 8 9 10
classSolution { public: boolcheckIfPangram(string sentence){ int state = 0; for (auto c : sentence) { state |= 1 << (c - 'a'); } return state == (1 << 26) - 1; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10
classSolution { publicbooleancheckIfPangram(String sentence) { intstate=0; for (inti=0; i < sentence.length(); i++) { charc= sentence.charAt(i); state |= 1 << (c - 'a'); } return state == (1 << 26) - 1; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9
publicclassSolution { publicboolCheckIfPangram(string sentence) { int state = 0; foreach (char c in sentence) { state |= 1 << (c - 'a'); } return state == (1 << 26) - 1; } }
[sol2-Golang]
1 2 3 4 5 6 7
funccheckIfPangram(sentence string)bool { state := 0 for _, c := range sentence { state |= 1 << (c - 'a') } return state == 1<<26-1 }
[sol2-JavaScript]
1 2 3 4 5 6 7 8
var checkIfPangram = function(sentence) { let state = 0; for (let i = 0; i < sentence.length; i++) { const c = sentence[i]; state |= 1 << (c.charCodeAt() - 'a'.charCodeAt()); } return state == (1 << 26) - 1; };
[sol2-C]
1 2 3 4 5 6 7
boolcheckIfPangram(char * sentence) { int state = 0; for (int i = 0; sentence[i] != '\0'; i++) { state |= 1 << (sentence[i] - 'a'); } return state == (1 << 26) - 1; }
复杂度分析
时间复杂度:O(n),其中 n 是 sentence 的长度。整个过程只需要遍历一次 sentence。