Skip to content

Commit 46c3e40

Browse files
committedJan 1, 2021
feat: add python and java solutions to leetcode problem: No.0383
1 parent 27d6e04 commit 46c3e40

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
- [反转字符串中的元音字母](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README.md)
6868
- [字符串转换整数 (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md)
69+
- [赎金信](/solution/0300-0399/0383.Ransom%20Note/README.md)
6970

7071
### 链表
7172

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6363

6464
- [Reverse Vowels of a String](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README_EN.md)
6565
- [String to Integer (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README_EN.md)
66+
- [Ransom Note](/solution/0300-0399/0383.Ransom%20Note/README_EN.md)
6667

6768
### Linked List
6869

‎solution/0300-0399/0383.Ransom Note/README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,47 @@ canConstruct("aa", "aab") -> true
2323

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

26+
用一个数组或字典 chars 存放 magazine 中每个字母出现的次数。遍历 ransomNote 中每个字母,判断 chars 是否包含即可。
27+
2628
<!-- tabs:start -->
2729

2830
### **Python3**
2931

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

3234
```python
33-
35+
class Solution:
36+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
37+
chars = {}
38+
for i in magazine:
39+
chars[i] = chars.get(i, 0) + 1
40+
for i in ransomNote:
41+
if not chars.get(i):
42+
return False
43+
chars[i] -= 1
44+
return True
3445
```
3546

3647
### **Java**
3748

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

4051
```java
41-
52+
class Solution {
53+
public boolean canConstruct(String ransomNote, String magazine) {
54+
int[] chars = new int[26];
55+
for (int i = 0, n = magazine.length(); i < n; ++i) {
56+
int idx = magazine.charAt(i) - 'a';
57+
++chars[idx];
58+
}
59+
for (int i = 0, n = ransomNote.length(); i < n; ++i) {
60+
int idx = ransomNote.charAt(i) - 'a';
61+
if (chars[idx] == 0) return false;
62+
--chars[idx];
63+
}
64+
return true;
65+
}
66+
}
4267
```
4368

4469
### **...**

‎solution/0300-0399/0383.Ransom Note/README_EN.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,36 @@ canConstruct("aa", "aab") -> true
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
46+
chars = {}
47+
for i in magazine:
48+
chars[i] = chars.get(i, 0) + 1
49+
for i in ransomNote:
50+
if not chars.get(i):
51+
return False
52+
chars[i] -= 1
53+
return True
4554
```
4655

4756
### **Java**
4857

4958
```java
50-
59+
class Solution {
60+
public boolean canConstruct(String ransomNote, String magazine) {
61+
int[] chars = new int[26];
62+
for (int i = 0, n = magazine.length(); i < n; ++i) {
63+
int idx = magazine.charAt(i) - 'a';
64+
++chars[idx];
65+
}
66+
for (int i = 0, n = ransomNote.length(); i < n; ++i) {
67+
int idx = ransomNote.charAt(i) - 'a';
68+
if (chars[idx] == 0) return false;
69+
--chars[idx];
70+
}
71+
return true;
72+
}
73+
}
5174
```
5275

5376
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean canConstruct(String ransomNote, String magazine) {
3+
int[] chars = new int[26];
4+
for (int i = 0, n = magazine.length(); i < n; ++i) {
5+
int idx = magazine.charAt(i) - 'a';
6+
++chars[idx];
7+
}
8+
for (int i = 0, n = ransomNote.length(); i < n; ++i) {
9+
int idx = ransomNote.charAt(i) - 'a';
10+
if (chars[idx] == 0) return false;
11+
--chars[idx];
12+
}
13+
return true;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
3+
chars = {}
4+
for i in magazine:
5+
chars[i] = chars.get(i, 0) + 1
6+
for i in ransomNote:
7+
if not chars.get(i):
8+
return False
9+
chars[i] -= 1
10+
return True

0 commit comments

Comments
 (0)
Please sign in to comment.