Skip to content

Commit ffd8742

Browse files
committed
feat: add solutions to lc problem: No.0383
No.0383.Ransom Note
1 parent 533169c commit ffd8742

File tree

5 files changed

+85
-44
lines changed

5 files changed

+85
-44
lines changed

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

+30-15
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<li>你可以假设两个字符串均只含有小写字母。</li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -57,13 +56,11 @@
5756
```python
5857
class Solution:
5958
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
60-
chars = {}
61-
for i in magazine:
62-
chars[i] = chars.get(i, 0) + 1
63-
for i in ransomNote:
64-
if not chars.get(i):
59+
counter = Counter(magazine)
60+
for c in ransomNote:
61+
if counter[c] <= 0:
6562
return False
66-
chars[i] -= 1
63+
counter[c] -= 1
6764
return True
6865
```
6966

@@ -74,15 +71,15 @@ class Solution:
7471
```java
7572
class Solution {
7673
public boolean canConstruct(String ransomNote, String magazine) {
77-
int[] chars = new int[26];
78-
for (int i = 0, n = magazine.length(); i < n; ++i) {
79-
int idx = magazine.charAt(i) - 'a';
80-
++chars[idx];
74+
int[] counter = new int[26];
75+
for (char c : magazine.toCharArray()) {
76+
++counter[c - 'a'];
8177
}
82-
for (int i = 0, n = ransomNote.length(); i < n; ++i) {
83-
int idx = ransomNote.charAt(i) - 'a';
84-
if (chars[idx] == 0) return false;
85-
--chars[idx];
78+
for (char c : ransomNote.toCharArray()) {
79+
if (counter[c - 'a'] <= 0) {
80+
return false;
81+
}
82+
--counter[c - 'a'];
8683
}
8784
return true;
8885
}
@@ -107,6 +104,24 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
107104
};
108105
```
109106

107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
bool canConstruct(string ransomNote, string magazine) {
113+
vector<int> counter(26);
114+
for (char c : magazine) ++counter[c - 'a'];
115+
for (char c : ransomNote)
116+
{
117+
if (counter[c - 'a'] <= 0) return false;
118+
--counter[c - 'a'];
119+
}
120+
return true;
121+
}
122+
};
123+
```
124+
110125
### **Go**
111126
112127
```go

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

+30-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<li>You may assume that both strings contain only lowercase letters.</li>
2727
</ul>
2828

29-
3029
## Solutions
3130

3231
<!-- tabs:start -->
@@ -36,13 +35,11 @@
3635
```python
3736
class Solution:
3837
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
39-
chars = {}
40-
for i in magazine:
41-
chars[i] = chars.get(i, 0) + 1
42-
for i in ransomNote:
43-
if not chars.get(i):
38+
counter = Counter(magazine)
39+
for c in ransomNote:
40+
if counter[c] <= 0:
4441
return False
45-
chars[i] -= 1
42+
counter[c] -= 1
4643
return True
4744
```
4845

@@ -51,15 +48,15 @@ class Solution:
5148
```java
5249
class Solution {
5350
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];
51+
int[] counter = new int[26];
52+
for (char c : magazine.toCharArray()) {
53+
++counter[c - 'a'];
5854
}
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];
55+
for (char c : ransomNote.toCharArray()) {
56+
if (counter[c - 'a'] <= 0) {
57+
return false;
58+
}
59+
--counter[c - 'a'];
6360
}
6461
return true;
6562
}
@@ -84,6 +81,24 @@ function canConstruct(ransomNote: string, magazine: string): boolean {
8481
};
8582
```
8683

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
bool canConstruct(string ransomNote, string magazine) {
90+
vector<int> counter(26);
91+
for (char c : magazine) ++counter[c - 'a'];
92+
for (char c : ransomNote)
93+
{
94+
if (counter[c - 'a'] <= 0) return false;
95+
--counter[c - 'a'];
96+
}
97+
return true;
98+
}
99+
};
100+
```
101+
87102
### **Go**
88103
89104
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool canConstruct(string ransomNote, string magazine) {
4+
vector<int> counter(26);
5+
for (char c : magazine) ++counter[c - 'a'];
6+
for (char c : ransomNote)
7+
{
8+
if (counter[c - 'a'] <= 0) return false;
9+
--counter[c - 'a'];
10+
}
11+
return true;
12+
}
13+
};

solution/0300-0399/0383.Ransom Note/Solution.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
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];
3+
int[] counter = new int[26];
4+
for (char c : magazine.toCharArray()) {
5+
++counter[c - 'a'];
76
}
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];
7+
for (char c : ransomNote.toCharArray()) {
8+
if (counter[c - 'a'] <= 0) {
9+
return false;
10+
}
11+
--counter[c - 'a'];
1212
}
1313
return true;
1414
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
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):
3+
counter = Counter(magazine)
4+
for c in ransomNote:
5+
if counter[c] <= 0:
86
return False
9-
chars[i] -= 1
7+
counter[c] -= 1
108
return True

0 commit comments

Comments
 (0)