File tree 4 files changed +106
-2
lines changed
solution/1800-1899/1832.Check if the Sentence Is Pangram
4 files changed +106
-2
lines changed Original file line number Diff line number Diff line change 38
38
<li><code>sentence</code> 由小写英语字母组成</li>
39
39
</ul >
40
40
41
-
42
41
## 解法
43
42
44
43
<!-- 这里可写通用的实现逻辑 -->
45
44
45
+ 转为 Set,判断 Set 长度是否等于 26。若是,说明是全字母句。也可以使用位运算。
46
+
46
47
<!-- tabs:start -->
47
48
48
49
### ** Python3**
49
50
50
51
<!-- 这里可写当前语言的特殊实现逻辑 -->
51
52
53
+ 集合去重并计数:
54
+
52
55
``` python
56
+ class Solution :
57
+ def checkIfPangram (self , sentence : str ) -> bool :
58
+ return len (set (sentence)) == 26
59
+ ```
60
+
61
+ 位运算:
53
62
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 == 0x 3ffffff :
71
+ return True
72
+ return False
54
73
```
55
74
56
75
### ** Java**
57
76
58
77
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
78
79
+ 集合去重并计数:
80
+
60
81
``` 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
+ ```
61
93
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
+ }
62
108
```
63
109
64
110
### ** ...**
Original file line number Diff line number Diff line change 32
32
<li><code>sentence</code> consists of lowercase English letters.</li>
33
33
</ul >
34
34
35
-
36
35
## Solutions
37
36
38
37
<!-- tabs:start -->
39
38
40
39
### ** Python3**
41
40
41
+ Set:
42
+
42
43
``` python
44
+ class Solution :
45
+ def checkIfPangram (self , sentence : str ) -> bool :
46
+ return len (set (sentence)) == 26
47
+ ```
43
48
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 == 0x 3ffffff :
59
+ return True
60
+ return False
44
61
```
45
62
46
63
### ** Java**
47
64
65
+ HashSet:
66
+
48
67
``` 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
+ ```
49
79
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
+ }
50
94
```
51
95
52
96
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ class Solution :
2
+ def checkIfPangram (self , sentence : str ) -> bool :
3
+ return len (set (sentence )) == 26
You can’t perform that action at this time.
0 commit comments