Skip to content

Commit dd62309

Browse files
committed
feat: add solutions to lc problem: No.0383
No.0383.Ransom Note
1 parent 1604e27 commit dd62309

File tree

7 files changed

+115
-121
lines changed

7 files changed

+115
-121
lines changed

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

+46-44
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848

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

51-
用一个数组或字典 chars 存放 magazine 中每个字母出现的次数。遍历 ransomNote 中每个字母,判断 chars 是否包含即可。
51+
**方法一:哈希表或数组**
52+
53+
我们可以用一个哈希表或长度为 $26$ 的数组 $cnt$ 记录字符串 `magazine` 中所有字符出现的次数。然后遍历字符串 `ransomNote`,对于其中的每个字符 $c$,我们将其从 $cnt$ 的次数减 $1$,如果减 $1$ 之后的次数小于 $0$,说明 $c$ 在 `magazine` 中出现的次数不够,因此无法构成 `ransomNote`,返回 $false$ 即可。
54+
55+
否则,遍历结束后,说明 `ransomNote` 中的每个字符都可以在 `magazine` 中找到对应的字符,因此返回 $true$。
56+
57+
时间复杂度 $O(m + n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别为字符串 `ransomNote``magazine` 的长度;而 $C$ 为字符集的大小,本题中 $C = 26$。
5258

5359
<!-- tabs:start -->
5460

@@ -59,11 +65,11 @@
5965
```python
6066
class Solution:
6167
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
62-
counter = Counter(magazine)
68+
cnt = Counter(magazine)
6369
for c in ransomNote:
64-
if counter[c] <= 0:
70+
cnt[c] -= 1
71+
if cnt[c] < 0:
6572
return False
66-
counter[c] -= 1
6773
return True
6874
```
6975

@@ -74,50 +80,34 @@ class Solution:
7480
```java
7581
class Solution {
7682
public boolean canConstruct(String ransomNote, String magazine) {
77-
int[] counter = new int[26];
78-
for (char c : magazine.toCharArray()) {
79-
++counter[c - 'a'];
83+
int[] cnt = new int[26];
84+
for (int i = 0; i < magazine.length(); ++i) {
85+
++cnt[magazine.charAt(i) - 'a'];
8086
}
81-
for (char c : ransomNote.toCharArray()) {
82-
if (counter[c - 'a'] <= 0) {
87+
for (int i = 0; i < ransomNote.length(); ++i) {
88+
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
8389
return false;
8490
}
85-
--counter[c - 'a'];
8691
}
8792
return true;
8893
}
8994
}
9095
```
9196

92-
### **TypeScript**
93-
94-
```ts
95-
function canConstruct(ransomNote: string, magazine: string): boolean {
96-
let counter = new Array(26).fill(0);
97-
let base = 'a'.charCodeAt(0);
98-
for (let s of magazine) {
99-
++counter[s.charCodeAt(0) - base];
100-
}
101-
for (let s of ransomNote) {
102-
let idx = s.charCodeAt(0) - base;
103-
if (counter[idx] == 0) return false;
104-
--counter[idx];
105-
}
106-
return true;
107-
}
108-
```
109-
11097
### **C++**
11198

11299
```cpp
113100
class Solution {
114101
public:
115102
bool canConstruct(string ransomNote, string magazine) {
116-
vector<int> counter(26);
117-
for (char c : magazine) ++counter[c - 'a'];
118-
for (char c : ransomNote) {
119-
if (counter[c - 'a'] <= 0) return false;
120-
--counter[c - 'a'];
103+
int cnt[26]{};
104+
for (char& c : magazine) {
105+
++cnt[c - 'a'];
106+
}
107+
for (char& c : ransomNote) {
108+
if (--cnt[c - 'a'] < 0) {
109+
return false;
110+
}
121111
}
122112
return true;
123113
}
@@ -128,25 +118,37 @@ public:
128118
129119
```go
130120
func canConstruct(ransomNote string, magazine string) bool {
131-
rc := make([]int, 26)
132-
for _, b := range ransomNote {
133-
rc[b-'a']++
134-
}
135-
136-
mc := make([]int, 26)
137-
for _, b := range magazine {
138-
mc[b-'a']++
121+
cnt := [26]int{}
122+
for _, c := range magazine {
123+
cnt[c-'a']++
139124
}
140-
141-
for i := 0; i < 26; i++ {
142-
if rc[i] > mc[i] {
125+
for _, c := range ransomNote {
126+
cnt[c-'a']--
127+
if cnt[c-'a'] < 0 {
143128
return false
144129
}
145130
}
146131
return true
147132
}
148133
```
149134

135+
### **TypeScript**
136+
137+
```ts
138+
function canConstruct(ransomNote: string, magazine: string): boolean {
139+
const cnt = new Array(26).fill(0);
140+
for (const c of magazine) {
141+
++cnt[c.charCodeAt(0) - 97];
142+
}
143+
for (const c of ransomNote) {
144+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
145+
return false;
146+
}
147+
}
148+
return true;
149+
}
150+
```
151+
150152
### **PHP**
151153

152154
```php

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

+39-43
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
```python
3737
class Solution:
3838
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
39-
counter = Counter(magazine)
39+
cnt = Counter(magazine)
4040
for c in ransomNote:
41-
if counter[c] <= 0:
41+
cnt[c] -= 1
42+
if cnt[c] < 0:
4243
return False
43-
counter[c] -= 1
4444
return True
4545
```
4646

@@ -49,50 +49,34 @@ class Solution:
4949
```java
5050
class Solution {
5151
public boolean canConstruct(String ransomNote, String magazine) {
52-
int[] counter = new int[26];
53-
for (char c : magazine.toCharArray()) {
54-
++counter[c - 'a'];
52+
int[] cnt = new int[26];
53+
for (int i = 0; i < magazine.length(); ++i) {
54+
++cnt[magazine.charAt(i) - 'a'];
5555
}
56-
for (char c : ransomNote.toCharArray()) {
57-
if (counter[c - 'a'] <= 0) {
56+
for (int i = 0; i < ransomNote.length(); ++i) {
57+
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
5858
return false;
5959
}
60-
--counter[c - 'a'];
6160
}
6261
return true;
6362
}
6463
}
6564
```
6665

67-
### **TypeScript**
68-
69-
```ts
70-
function canConstruct(ransomNote: string, magazine: string): boolean {
71-
let counter = new Array(26).fill(0);
72-
let base = 'a'.charCodeAt(0);
73-
for (let s of magazine) {
74-
++counter[s.charCodeAt(0) - base];
75-
}
76-
for (let s of ransomNote) {
77-
let idx = s.charCodeAt(0) - base;
78-
if (counter[idx] == 0) return false;
79-
--counter[idx];
80-
}
81-
return true;
82-
}
83-
```
84-
8566
### **C++**
8667

8768
```cpp
8869
class Solution {
8970
public:
9071
bool canConstruct(string ransomNote, string magazine) {
91-
vector<int> counter(26);
92-
for (char c : magazine) ++counter[c - 'a'];
93-
for (char c : ransomNote) {
94-
if (counter[c - 'a'] <= 0) return false;
95-
--counter[c - 'a'];
72+
int cnt[26]{};
73+
for (char& c : magazine) {
74+
++cnt[c - 'a'];
75+
}
76+
for (char& c : ransomNote) {
77+
if (--cnt[c - 'a'] < 0) {
78+
return false;
79+
}
9680
}
9781
return true;
9882
}
@@ -103,25 +87,37 @@ public:
10387
10488
```go
10589
func canConstruct(ransomNote string, magazine string) bool {
106-
rc := make([]int, 26)
107-
for _, b := range ransomNote {
108-
rc[b-'a']++
90+
cnt := [26]int{}
91+
for _, c := range magazine {
92+
cnt[c-'a']++
10993
}
110-
111-
mc := make([]int, 26)
112-
for _, b := range magazine {
113-
mc[b-'a']++
114-
}
115-
116-
for i := 0; i < 26; i++ {
117-
if rc[i] > mc[i] {
94+
for _, c := range ransomNote {
95+
cnt[c-'a']--
96+
if cnt[c-'a'] < 0 {
11897
return false
11998
}
12099
}
121100
return true
122101
}
123102
```
124103

104+
### **TypeScript**
105+
106+
```ts
107+
function canConstruct(ransomNote: string, magazine: string): boolean {
108+
const cnt = new Array(26).fill(0);
109+
for (const c of magazine) {
110+
++cnt[c.charCodeAt(0) - 97];
111+
}
112+
for (const c of ransomNote) {
113+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
```
120+
125121
### **PHP**
126122

127123
```php

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
class Solution {
22
public:
33
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-
if (counter[c - 'a'] <= 0) return false;
8-
--counter[c - 'a'];
4+
int cnt[26]{};
5+
for (char& c : magazine) {
6+
++cnt[c - 'a'];
7+
}
8+
for (char& c : ransomNote) {
9+
if (--cnt[c - 'a'] < 0) {
10+
return false;
11+
}
912
}
1013
return true;
1114
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
func canConstruct(ransomNote string, magazine string) bool {
2-
rc := make([]int, 26)
3-
for _, b := range ransomNote {
4-
rc[b-'a']++
2+
cnt := [26]int{}
3+
for _, c := range magazine {
4+
cnt[c-'a']++
55
}
6-
7-
mc := make([]int, 26)
8-
for _, b := range magazine {
9-
mc[b-'a']++
10-
}
11-
12-
for i := 0; i < 26; i++ {
13-
if rc[i] > mc[i] {
6+
for _, c := range ransomNote {
7+
cnt[c-'a']--
8+
if cnt[c-'a'] < 0 {
149
return false
1510
}
1611
}
1712
return true
18-
}
13+
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
22
public boolean canConstruct(String ransomNote, String magazine) {
3-
int[] counter = new int[26];
4-
for (char c : magazine.toCharArray()) {
5-
++counter[c - 'a'];
3+
int[] cnt = new int[26];
4+
for (int i = 0; i < magazine.length(); ++i) {
5+
++cnt[magazine.charAt(i) - 'a'];
66
}
7-
for (char c : ransomNote.toCharArray()) {
8-
if (counter[c - 'a'] <= 0) {
7+
for (int i = 0; i < ransomNote.length(); ++i) {
8+
if (--cnt[ransomNote.charAt(i) - 'a'] < 0) {
99
return false;
1010
}
11-
--counter[c - 'a'];
1211
}
1312
return true;
1413
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
3-
counter = Counter(magazine)
3+
cnt = Counter(magazine)
44
for c in ransomNote:
5-
if counter[c] <= 0:
5+
cnt[c] -= 1
6+
if cnt[c] < 0:
67
return False
7-
counter[c] -= 1
88
return True
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function canConstruct(ransomNote: string, magazine: string): boolean {
2-
let counter = new Array(26).fill(0);
3-
let base = 'a'.charCodeAt(0);
4-
for (let s of magazine) {
5-
++counter[s.charCodeAt(0) - base];
2+
const cnt = new Array(26).fill(0);
3+
for (const c of magazine) {
4+
++cnt[c.charCodeAt(0) - 97];
65
}
7-
for (let s of ransomNote) {
8-
let idx = s.charCodeAt(0) - base;
9-
if (counter[idx] == 0) return false;
10-
--counter[idx];
6+
for (const c of ransomNote) {
7+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
8+
return false;
9+
}
1110
}
1211
return true;
1312
}

0 commit comments

Comments
 (0)