Skip to content

Commit

Permalink
feat: add leetcode solutions: No.1832. Check if the Sentence is Pangram
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 20, 2021
1 parent 18093d0 commit 9945e3c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,73 @@
<li><code>sentence</code> 由小写英语字母组成</li>
</ul>


## 解法

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

转为 Set,判断 Set 长度是否等于 26。若是,说明是全字母句。也可以使用位运算。

<!-- tabs:start -->

### **Python3**

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

集合去重并计数:

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

位运算:

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

### **Java**

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

集合去重并计数:

```java
class Solution {
public boolean checkIfPangram(String sentence) {
Set<Character> s = new HashSet<>();
for (int i = 0; i < sentence.length(); ++i) {
s.add(sentence.charAt(i));
if (s.size() == 26) return true;
}
return false;
}
}
```

位运算:

```java
class Solution {
public boolean checkIfPangram(String sentence) {
int res = 0;
for (int i = 0; i < sentence.length(); ++i) {
int diff = sentence.charAt(i) - 'a';
res |= (1 << diff);
if (res == 0x3ffffff) return true;
}
return false;
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,65 @@
<li><code>sentence</code> consists of lowercase English letters.</li>
</ul>


## Solutions

<!-- tabs:start -->

### **Python3**

Set:

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

Bit Manipulation:

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

### **Java**

HashSet:

```java
class Solution {
public boolean checkIfPangram(String sentence) {
Set<Character> s = new HashSet<>();
for (int i = 0; i < sentence.length(); ++i) {
s.add(sentence.charAt(i));
if (s.size() == 26) return true;
}
return false;
}
}
```

Bit Manipulation:

```java
class Solution {
public boolean checkIfPangram(String sentence) {
int res = 0;
for (int i = 0; i < sentence.length(); ++i) {
int diff = sentence.charAt(i) - 'a';
res |= (1 << diff);
if (res == 0x3ffffff) return true;
}
return false;
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public boolean checkIfPangram(String sentence) {
int res = 0;
for (int i = 0; i < sentence.length(); ++i) {
int diff = sentence.charAt(i) - 'a';
res |= (1 << diff);
if (res == 0x3ffffff) return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
return len(set(sentence)) == 26

0 comments on commit 9945e3c

Please sign in to comment.