Skip to content

Commit 3c6853f

Browse files
committed
feat: add solutions to lc problem: No.1832
No.1832.Check if the Sentence Is Pangram
1 parent dfd08ff commit 3c6853f

File tree

6 files changed

+118
-100
lines changed

6 files changed

+118
-100
lines changed

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

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,69 +42,70 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
转为 Set,判断 Set 长度是否等于 26。若是,说明是全字母句。也可以使用位运算。
45+
**方法一:数组或哈希表**
46+
47+
遍历字符串 `sentence`,用数组或哈希表记录出现过的字母,最后判断数组或哈希表中是否有 $26$ 个字母即可。
48+
49+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 `sentence` 的长度,而 $C$ 为字符集大小。本题中 $C = 26$。
50+
51+
**方法二:位运算**
52+
53+
我们也可以用一个整数 $mask$ 记录出现过的字母,其中 $mask$ 的第 $i$ 位表示第 $i$ 个字母是否出现过。
54+
55+
最后判断 $mask$ 的二进制表示中是否有 $26$ 个 $1$,也即判断 $mask$ 是否等于 $2^{26} - 1$。若是,返回 `true`,否则返回 `false`
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 `sentence` 的长度。
4658

4759
<!-- tabs:start -->
4860

4961
### **Python3**
5062

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

53-
集合去重并计数:
54-
5565
```python
5666
class Solution:
5767
def checkIfPangram(self, sentence: str) -> bool:
5868
return len(set(sentence)) == 26
5969
```
6070

61-
位运算:
62-
6371
```python
6472
class Solution:
6573
def checkIfPangram(self, sentence: str) -> bool:
66-
res = 0
74+
mask = 0
6775
for c in sentence:
68-
res |= (1 << (ord(c) - ord('a')))
69-
if res == 0x3ffffff:
70-
return True
71-
return False
76+
mask |= 1 << (ord(c) - ord('a'))
77+
return mask == (1 << 26) - 1
7278
```
7379

7480
### **Java**
7581

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

78-
集合去重并计数:
79-
8084
```java
8185
class Solution {
8286
public boolean checkIfPangram(String sentence) {
83-
Set<Character> s = new HashSet<>();
84-
for (char c : sentence.toCharArray()) {
85-
s.add(c);
86-
if (s.size() == 26) {
87-
return true;
87+
boolean[] vis = new boolean[26];
88+
for (int i = 0; i < sentence.length(); ++i) {
89+
vis[sentence.charAt(i) - 'a'] = true;
90+
}
91+
for (boolean v : vis) {
92+
if (!v) {
93+
return false;
8894
}
8995
}
90-
return false;
96+
return true;
9197
}
9298
}
9399
```
94100

95-
位运算:
96-
97101
```java
98102
class Solution {
99103
public boolean checkIfPangram(String sentence) {
100-
int res = 0;
101-
for (char c : sentence.toCharArray()) {
102-
res |= (1 << (c - 'a'));
103-
if (res == 0x3ffffff) {
104-
return true;
105-
}
104+
int mask = 0;
105+
for (int i = 0; i < sentence.length(); ++i) {
106+
mask |= 1 << (sentence.charAt(i) - 'a');
106107
}
107-
return false;
108+
return mask == (1 << 26) - 1;
108109
}
109110
}
110111
```
@@ -115,12 +116,21 @@ class Solution {
115116
class Solution {
116117
public:
117118
bool checkIfPangram(string sentence) {
118-
int res = 0;
119-
for (char c : sentence) {
120-
res |= (1 << (c - 'a'));
121-
if (res == 0x3ffffff) return true;
122-
}
123-
return false;
119+
int vis[26] = {0};
120+
for (char& c : sentence) vis[c - 'a'] = 1;
121+
for (int& v : vis) if (!v) return false;
122+
return true;
123+
}
124+
};
125+
```
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
bool checkIfPangram(string sentence) {
131+
int mask = 0;
132+
for (char& c : sentence) mask |= 1 << (c - 'a');
133+
return mask == (1 << 26) - 1;
124134
}
125135
};
126136
```
@@ -129,14 +139,26 @@ public:
129139

130140
```go
131141
func checkIfPangram(sentence string) bool {
132-
res := 0
142+
vis := [26]bool{}
133143
for _, c := range sentence {
134-
res |= (1 << (c - 'a'))
135-
if res == 0x3ffffff {
136-
return true
144+
vis[c-'a'] = true
145+
}
146+
for _, v := range vis {
147+
if !v {
148+
return false
137149
}
138150
}
139-
return false
151+
return true
152+
}
153+
```
154+
155+
```go
156+
func checkIfPangram(sentence string) bool {
157+
mask := 0
158+
for _, c := range sentence {
159+
mask |= 1 << int(c-'a')
160+
}
161+
return mask == 1<<26-1
140162
}
141163
```
142164

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

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,59 +38,48 @@
3838

3939
### **Python3**
4040

41-
Set:
42-
4341
```python
4442
class Solution:
4543
def checkIfPangram(self, sentence: str) -> bool:
4644
return len(set(sentence)) == 26
4745
```
4846

49-
Bit Manipulation:
50-
5147
```python
5248
class Solution:
5349
def checkIfPangram(self, sentence: str) -> bool:
54-
res = 0
50+
mask = 0
5551
for c in sentence:
56-
res |= (1 << (ord(c) - ord('a')))
57-
if res == 0x3ffffff:
58-
return True
59-
return False
52+
mask |= 1 << (ord(c) - ord('a'))
53+
return mask == (1 << 26) - 1
6054
```
6155

6256
### **Java**
6357

64-
HashSet:
65-
6658
```java
6759
class Solution {
6860
public boolean checkIfPangram(String sentence) {
69-
Set<Character> s = new HashSet<>();
70-
for (char c : sentence.toCharArray()) {
71-
s.add(c);
72-
if (s.size() == 26) {
73-
return true;
61+
boolean[] vis = new boolean[26];
62+
for (int i = 0; i < sentence.length(); ++i) {
63+
vis[sentence.charAt(i) - 'a'] = true;
64+
}
65+
for (boolean v : vis) {
66+
if (!v) {
67+
return false;
7468
}
7569
}
76-
return false;
70+
return true;
7771
}
7872
}
7973
```
8074

81-
Bit Manipulation:
82-
8375
```java
8476
class Solution {
8577
public boolean checkIfPangram(String sentence) {
86-
int res = 0;
87-
for (char c : sentence.toCharArray()) {
88-
res |= (1 << (c - 'a'));
89-
if (res == 0x3ffffff) {
90-
return true;
91-
}
78+
int mask = 0;
79+
for (int i = 0; i < sentence.length(); ++i) {
80+
mask |= 1 << (sentence.charAt(i) - 'a');
9281
}
93-
return false;
82+
return mask == (1 << 26) - 1;
9483
}
9584
}
9685
```
@@ -101,12 +90,21 @@ class Solution {
10190
class Solution {
10291
public:
10392
bool checkIfPangram(string sentence) {
104-
int res = 0;
105-
for (char c : sentence) {
106-
res |= (1 << (c - 'a'));
107-
if (res == 0x3ffffff) return true;
108-
}
109-
return false;
93+
int vis[26] = {0};
94+
for (char& c : sentence) vis[c - 'a'] = 1;
95+
for (int& v : vis) if (!v) return false;
96+
return true;
97+
}
98+
};
99+
```
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool checkIfPangram(string sentence) {
105+
int mask = 0;
106+
for (char& c : sentence) mask |= 1 << (c - 'a');
107+
return mask == (1 << 26) - 1;
110108
}
111109
};
112110
```
@@ -115,14 +113,26 @@ public:
115113

116114
```go
117115
func checkIfPangram(sentence string) bool {
118-
res := 0
116+
vis := [26]bool{}
119117
for _, c := range sentence {
120-
res |= (1 << (c - 'a'))
121-
if res == 0x3ffffff {
122-
return true
118+
vis[c-'a'] = true
119+
}
120+
for _, v := range vis {
121+
if !v {
122+
return false
123123
}
124124
}
125-
return false
125+
return true
126+
}
127+
```
128+
129+
```go
130+
func checkIfPangram(sentence string) bool {
131+
mask := 0
132+
for _, c := range sentence {
133+
mask |= 1 << int(c-'a')
134+
}
135+
return mask == 1<<26-1
126136
}
127137
```
128138

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class Solution {
22
public:
33
bool checkIfPangram(string sentence) {
4-
int res = 0;
5-
for (char c : sentence) {
6-
res |= (1 << (c - 'a'));
7-
if (res == 0x3ffffff) return true;
8-
}
9-
return false;
4+
int mask = 0;
5+
for (char& c : sentence) mask |= 1 << (c - 'a');
6+
return mask == (1 << 26) - 1;
107
}
118
};
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
func checkIfPangram(sentence string) bool {
2-
res := 0
2+
mask := 0
33
for _, c := range sentence {
4-
res |= (1 << (c - 'a'))
5-
if res == 0x3ffffff {
6-
return true
7-
}
4+
mask |= 1 << int(c-'a')
85
}
9-
return false
6+
return mask == 1<<26-1
107
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
class Solution {
22
public boolean checkIfPangram(String sentence) {
3-
int res = 0;
4-
for (char c : sentence.toCharArray()) {
5-
res |= (1 << (c - 'a'));
6-
if (res == 0x3ffffff) {
7-
return true;
8-
}
3+
int mask = 0;
4+
for (int i = 0; i < sentence.length(); ++i) {
5+
mask |= 1 << (sentence.charAt(i) - 'a');
96
}
10-
return false;
7+
return mask == (1 << 26) - 1;
118
}
129
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
class Solution:
22
def checkIfPangram(self, sentence: str) -> bool:
3-
res = 0
4-
for c in sentence:
5-
res |= 1 << (ord(c) - ord('a'))
6-
if res == 0x3FFFFFF:
7-
return True
8-
return False
3+
return len(set(sentence)) == 26

0 commit comments

Comments
 (0)