1
- #383. Ransom Note - Best Practices of LeetCode Solutions
2
- LeetCode link:[ 383. Ransom Note] ( https://leetcode.com/problems/ransom-note ) ,
3
- [ 383. 赎金信] ( https://leetcode.cn/problems/ransom-note )
1
+ 原文链接:[ coding5.com - 力扣题解最佳实践] ( https://coding5.com/zh/leetcode/383-ransom-note )
4
2
5
- [ 中文题解 ] ( #中文题解 )
3
+ # 383. 赎金信 - 力扣题解最佳实践
6
4
7
- ##LeetCode problem description
8
- Given two strings` ransomNote ` and` magazine ` , return` true ` if` ransomNote ` can be constructed by using the letters from` magazine ` and` false ` otherwise.
5
+ 力扣链接:[ 383. 赎金信] ( https://leetcode.cn/problems/ransom-note ) , 难度:** 简单** 。
9
6
10
- Each letter in ` magazine ` can only be used once in ` ransomNote ` .
7
+ ## 力扣“383. 赎金信”问题描述
11
8
12
- Difficulty: ** Easy **
9
+ 给你两个字符串: ` ransomNote ` 和 ` magazine ` ,判断 ` ransomNote ` 能不能由 ` magazine ` 里面的字符构成。
13
10
14
- ###[ Example 1]
15
- ** Input** :` ransomNote = "a", magazine = "b" `
11
+ 如果可以,返回` true ` ;否则返回` false ` 。
16
12
17
- ** Output ** : ` false `
13
+ ` magazine ` 中的每个字符只能在 ` ransomNote ` 中使用一次。
18
14
19
- ###[ Example 2]
20
- ** Input** :` ransomNote = "aa", magazine = "ab" `
15
+ ###[ 示例 1]
21
16
22
- ** Output ** :` false `
17
+ ** 输入 ** :` ransomNote = "a", magazine = "b" `
23
18
24
- ### [ Example 3 ]
25
- ** Input ** : ` ransomNote = "aa", magazine = "aab" `
19
+ ** 输出 ** : ` false `
20
+ ### [ 示例 2 ]
26
21
27
- ** Output ** :` true `
22
+ ** 输入 ** :` ransomNote = "aa", magazine = "ab" `
28
23
29
- ###[ Constraints]
30
- - ` 1 <= ransomNote.length, magazine.length <= 100000 `
31
- - ` ransomNote ` and` magazine ` consist of lowercase English letters.
24
+ ** 输出** :` false `
25
+ ###[ 示例 3]
32
26
33
- ##Intuition
34
- [ 中文题解] ( #中文题解 )
27
+ ** 输入** :` ransomNote = "aa", magazine = "aab" `
35
28
36
- 1 . This question is equivalent to asking whether` magazine ` can contain all the characters in` ransomNote ` .
37
- 2 . First count the characters in` magazine ` , and store the results in` Map ` .
38
- 3 . Then, traverse` ransomNote ` and perform reverse operations on the data in` Map ` . If the count of a character is less than 0, return` false ` .
29
+ ** 输出** :` true `
30
+ ###[ 约束]
39
31
40
- ##Steps
41
- 1 . First count the characters in` magazine ` , and store the results in` Map ` .
42
- ``` javascript
43
- charToCount= new Map ()
32
+ - ` 1 <= ransomNote.length, magazine.length <= 10^5 `
33
+ - ` ransomNote ` 和` magazine ` 由小写英文字母组成
44
34
45
- for (characterin magazine) {
46
- charToCount[character]+= 1
47
- }
48
- ```
35
+ ##思路
49
36
50
- 2 . Then, traverse` ransomNote ` and perform reverse operations on the data in` Map ` . If the count of a character is less than 0, return` false ` .
51
- ``` javascript
52
- charToCount= new Map ()
37
+ 1 . 本题等同于求` magazine ` 是否能包含` ransomNote ` 中的所有字符。
38
+ 2 . 先对` magazine ` 进行统计,得出每个字符对应的字数,结果存储在` Map ` 中。每一次都是一个加一的操作。
39
+ 3 . 下一步做什么?
40
+ <details ><summary >点击查看答案</summary ><p >遍历`ransomNote`,对当前字符对应的数量进行减一操作(反向操作)。如果某个字符的数量小于0,则返回`false`。</p ></details >
53
41
54
- for (characterin magazine) {
55
- charToCount[character]+= 1
56
- }
42
+ ##步骤
57
43
58
- for (characterin ransomNote) {
59
- charToCount[character]-= 1
44
+ 1 . 先对` magazine ` 进行字符和字数统计,结果存储在` Map ` 中。
60
45
61
- if (charToCount[character]< 0 ) {
62
- return false
63
- }
64
- }
46
+ ```javascript
47
+ charToCount = new Map()
65
48
66
- return true
67
- ```
49
+ for (character in magazine) {
50
+ charToCount[character] += 1
51
+ }
52
+ ```
53
+
54
+ 2 . 然后,遍历` ransomNote ` ,并对` Map ` 中的数据进行反向操作。如果某个字符的字数小于0,则返回` false ` 。
68
55
69
- ##Complexity
70
- * Time:` O(n) ` .
71
- * Space:` O(n) ` .
56
+ ```javascript
57
+ charToCount = new Map()
58
+
59
+ for (character in magazine) {
60
+ charToCount[character] += 1
61
+ }
62
+
63
+ for (character in ransomNote) {
64
+ charToCount[character] -= 1
65
+
66
+ if (charToCount[character] < 0) {
67
+ return false
68
+ }
69
+ }
70
+
71
+ return true
72
+ ```
73
+
74
+ ##复杂度
75
+
76
+ - 时间复杂度:` O(N) ` .
77
+ - 空间复杂度:` O(N) ` .
72
78
73
79
##Java
80
+
74
81
``` java
75
82
class Solution {
76
83
public boolean canConstruct (String ransomNote ,String magazine ) {
@@ -94,6 +101,7 @@ class Solution {
94
101
```
95
102
96
103
##Python
104
+
97
105
``` python
98
106
# from collections import defaultdict
99
107
@@ -113,12 +121,8 @@ class Solution:
113
121
return True
114
122
```
115
123
116
- ##C++
117
- ``` cpp
118
- // Welcome to create a PR to complete the code of this language, thanks!
119
- ```
120
-
121
124
##JavaScript
125
+
122
126
``` javascript
123
127
var canConstruct = function (ransomNote ,magazine ) {
124
128
const charToCount = new Map ()
@@ -140,6 +144,7 @@ var canConstruct = function (ransomNote, magazine) {
140
144
```
141
145
142
146
##C#
147
+
143
148
``` c#
144
149
public class Solution
145
150
{
@@ -165,91 +170,8 @@ public class Solution
165
170
}
166
171
```
167
172
168
- ##Go
169
- ``` go
170
- // Welcome to create a PR to complete the code of this language, thanks!
171
- ```
172
-
173
- ##Ruby
174
- ``` ruby
175
- # Welcome to create a PR to complete the code of this language, thanks!
176
- ```
177
-
178
- ##C
179
- ``` c
180
- // Welcome to create a PR to complete the code of this language, thanks!
181
- ```
182
-
183
- ##Kotlin
184
- ``` kotlin
185
- // Welcome to create a PR to complete the code of this language, thanks!
186
- ```
187
-
188
- ##Swift
189
- ``` swift
190
- // Welcome to create a PR to complete the code of this language, thanks!
191
- ```
192
-
193
- ##Rust
194
- ``` rust
195
- // Welcome to create a PR to complete the code of this language, thanks!
196
- ```
197
-
198
173
##Other languages
199
- ```
200
- // Welcome to create a PR to complete the code of this language, thanks!
201
- ```
202
-
203
- ##力扣问题描述
204
- [ 383. 赎金信] ( https://leetcode.cn/problems/ransom-note ) ,难度:** 简单** 。
205
-
206
- 给你两个字符串:` ransomNote ` 和` magazine ` ,判断` ransomNote ` 能不能由` magazine ` 里面的字符构成。
207
-
208
- 如果可以,返回` true ` ;否则返回` false ` 。
209
-
210
- ` magazine ` 中的每个字符只能在` ransomNote ` 中使用一次。
211
-
212
- ###[ 示例 2]
213
- ** 输入** :` ransomNote = "aa", magazine = "ab" `
214
-
215
- ** 输出** :` false `
216
-
217
- ###[ 示例 3]
218
- ** 输入** :` ransomNote = "aa", magazine = "aab" `
219
174
220
- ** 输出** :` true `
221
-
222
- #中文题解
223
- ##思路
224
- 1 . 本题等同于求` magazine ` 是否能包含` ransomNote ` 中的所有字符。
225
- 2 . 先对` magazine ` 进行字符和字数统计,结果存储在` Map ` 中。
226
- 3 . 然后,遍历` ransomNote ` ,并对` Map ` 中的数据进行反向操作。如果某个字符的字数小于0,则返回` false ` 。
227
-
228
- ##步骤
229
- 1 . 先对` magazine ` 进行字符和字数统计,结果存储在` Map ` 中。
230
- ``` javascript
231
- charToCount= new Map ()
232
-
233
- for (characterin magazine) {
234
- charToCount[character]+= 1
235
- }
236
- ```
237
-
238
- 2 . 然后,遍历` ransomNote ` ,并对` Map ` 中的数据进行反向操作。如果某个字符的字数小于0,则返回` false ` 。
239
- ``` javascript
240
- charToCount= new Map ()
241
-
242
- for (characterin magazine) {
243
- charToCount[character]+= 1
244
- }
245
-
246
- for (characterin ransomNote) {
247
- charToCount[character]-= 1
248
-
249
- if (charToCount[character]< 0 ) {
250
- return false
251
- }
252
- }
253
-
254
- return true
175
+ ``` java
176
+ // Welcome to create a PR to complete the code of this language, thanks!
255
177
```