Skip to content

Commit 9945e3c

Browse files
committed
feat: add leetcode solutions: No.1832. Check if the Sentence is Pangram
1 parent 18093d0 commit 9945e3c

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

solution/1800-1899/1832.Check if the Sentence Is Pangram/README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,73 @@
3838
<li><code>sentence</code> 由小写英语字母组成</li>
3939
</ul>
4040

41-
4241
## 解法
4342

4443
<!-- 这里可写通用的实现逻辑 -->
4544

45+
转为 Set,判断 Set 长度是否等于 26。若是,说明是全字母句。也可以使用位运算。
46+
4647
<!-- tabs:start -->
4748

4849
### **Python3**
4950

5051
<!-- 这里可写当前语言的特殊实现逻辑 -->
5152

53+
集合去重并计数:
54+
5255
```python
56+
class Solution:
57+
def checkIfPangram(self, sentence: str) -> bool:
58+
return len(set(sentence)) == 26
59+
```
60+
61+
位运算:
5362

63+
```python
64+
class Solution:
65+
def checkIfPangram(self, sentence: str) -> bool:
66+
res = 0
67+
for c in sentence:
68+
diff = ord(c) - ord('a')
69+
res |= (1 << diff)
70+
if res == 0x3ffffff:
71+
return True
72+
return False
5473
```
5574

5675
### **Java**
5776

5877
<!-- 这里可写当前语言的特殊实现逻辑 -->
5978

79+
集合去重并计数:
80+
6081
```java
82+
class Solution {
83+
public boolean checkIfPangram(String sentence) {
84+
Set<Character> s = new HashSet<>();
85+
for (int i = 0; i < sentence.length(); ++i) {
86+
s.add(sentence.charAt(i));
87+
if (s.size() == 26) return true;
88+
}
89+
return false;
90+
}
91+
}
92+
```
6193

94+
位运算:
95+
96+
```java
97+
class Solution {
98+
public boolean checkIfPangram(String sentence) {
99+
int res = 0;
100+
for (int i = 0; i < sentence.length(); ++i) {
101+
int diff = sentence.charAt(i) - 'a';
102+
res |= (1 << diff);
103+
if (res == 0x3ffffff) return true;
104+
}
105+
return false;
106+
}
107+
}
62108
```
63109

64110
### **...**

solution/1800-1899/1832.Check if the Sentence Is Pangram/README_EN.md

+45-1
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,65 @@
3232
<li><code>sentence</code> consists of lowercase English letters.</li>
3333
</ul>
3434

35-
3635
## Solutions
3736

3837
<!-- tabs:start -->
3938

4039
### **Python3**
4140

41+
Set:
42+
4243
```python
44+
class Solution:
45+
def checkIfPangram(self, sentence: str) -> bool:
46+
return len(set(sentence)) == 26
47+
```
4348

49+
Bit Manipulation:
50+
51+
```python
52+
class Solution:
53+
def checkIfPangram(self, sentence: str) -> bool:
54+
res = 0
55+
for c in sentence:
56+
diff = ord(c) - ord('a')
57+
res |= (1 << diff)
58+
if res == 0x3ffffff:
59+
return True
60+
return False
4461
```
4562

4663
### **Java**
4764

65+
HashSet:
66+
4867
```java
68+
class Solution {
69+
public boolean checkIfPangram(String sentence) {
70+
Set<Character> s = new HashSet<>();
71+
for (int i = 0; i < sentence.length(); ++i) {
72+
s.add(sentence.charAt(i));
73+
if (s.size() == 26) return true;
74+
}
75+
return false;
76+
}
77+
}
78+
```
4979

80+
Bit Manipulation:
81+
82+
```java
83+
class Solution {
84+
public boolean checkIfPangram(String sentence) {
85+
int res = 0;
86+
for (int i = 0; i < sentence.length(); ++i) {
87+
int diff = sentence.charAt(i) - 'a';
88+
res |= (1 << diff);
89+
if (res == 0x3ffffff) return true;
90+
}
91+
return false;
92+
}
93+
}
5094
```
5195

5296
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean checkIfPangram(String sentence) {
3+
int res = 0;
4+
for (int i = 0; i < sentence.length(); ++i) {
5+
int diff = sentence.charAt(i) - 'a';
6+
res |= (1 << diff);
7+
if (res == 0x3ffffff) return true;
8+
}
9+
return false;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def checkIfPangram(self, sentence: str) -> bool:
3+
return len(set(sentence)) == 26

0 commit comments

Comments
 (0)