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 )
42
5- [ 中文题解 ] ( #中文题解 )
3+ # 383. 赎金信 - 力扣题解最佳实践
64
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 ) , 难度:** 简单** 。
96
10- Each letter in ` magazine ` can only be used once in ` ransomNote ` .
7+ ## 力扣“383. 赎金信”问题描述
118
12- Difficulty: ** Easy **
9+ 给你两个字符串: ` ransomNote ` 和 ` magazine ` ,判断 ` ransomNote ` 能不能由 ` magazine ` 里面的字符构成。
1310
14- ### [ Example 1]
15- ** Input** : ` ransomNote = "a", magazine = "b" `
11+ 如果可以,返回 ` true ` ;否则返回 ` false ` 。
1612
17- ** Output ** : ` false `
13+ ` magazine ` 中的每个字符只能在 ` ransomNote ` 中使用一次。
1814
19- ### [ Example 2]
20- ** Input** : ` ransomNote = "aa", magazine = "ab" `
15+ ### [ 示例 1]
2116
22- ** Output ** : ` false `
17+ ** 输入 ** : ` ransomNote = "a", magazine = "b" `
2318
24- ### [ Example 3 ]
25- ** Input ** : ` ransomNote = "aa", magazine = "aab" `
19+ ** 输出 ** : ` false `
20+ ### [ 示例 2 ]
2621
27- ** Output ** : ` true `
22+ ** 输入 ** : ` ransomNote = "aa", magazine = "ab" `
2823
29- ### [ Constraints]
30- - ` 1 <= ransomNote.length, magazine.length <= 100000 `
31- - ` ransomNote ` and ` magazine ` consist of lowercase English letters.
24+ ** 输出** : ` false `
25+ ### [ 示例 3]
3226
33- ## Intuition
34- [ 中文题解] ( #中文题解 )
27+ ** 输入** : ` ransomNote = "aa", magazine = "aab" `
3528
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+ ### [ 约束]
3931
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 ` 由小写英文字母组成
4434
45- for (character in magazine) {
46- charToCount[character] += 1
47- }
48- ```
35+ ## 思路
4936
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 >
5341
54- for (character in magazine) {
55- charToCount[character] += 1
56- }
42+ ## 步骤
5743
58- for (character in ransomNote) {
59- charToCount[character] -= 1
44+ 1 . 先对` magazine ` 进行字符和字数统计,结果存储在` Map ` 中。
6045
61- if (charToCount[character] < 0 ) {
62- return false
63- }
64- }
46+ ```javascript
47+ charToCount = new Map()
6548
66- return true
67- ```
49+ for (character in magazine) {
50+ charToCount[character] += 1
51+ }
52+ ```
53+
54+ 2 . 然后,遍历` ransomNote ` ,并对` Map ` 中的数据进行反向操作。如果某个字符的字数小于0,则返回` false ` 。
6855
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) ` .
7278
7379## Java
80+
7481``` java
7582class Solution {
7683 public boolean canConstruct (String ransomNote , String magazine ) {
@@ -94,6 +101,7 @@ class Solution {
94101```
95102
96103## Python
104+
97105``` python
98106# from collections import defaultdict
99107
@@ -113,12 +121,8 @@ class Solution:
113121 return True
114122```
115123
116- ## C++
117- ``` cpp
118- // Welcome to create a PR to complete the code of this language, thanks!
119- ```
120-
121124## JavaScript
125+
122126``` javascript
123127var canConstruct = function (ransomNote , magazine ) {
124128 const charToCount = new Map ()
@@ -140,6 +144,7 @@ var canConstruct = function (ransomNote, magazine) {
140144```
141145
142146## C#
147+
143148``` c#
144149public class Solution
145150{
@@ -165,91 +170,8 @@ public class Solution
165170}
166171```
167172
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-
198173## 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" `
219174
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 (character in magazine) {
234- charToCount[character] += 1
235- }
236- ```
237-
238- 2 . 然后,遍历` ransomNote ` ,并对` Map ` 中的数据进行反向操作。如果某个字符的字数小于0,则返回` false ` 。
239- ``` javascript
240- charToCount = new Map ()
241-
242- for (character in magazine) {
243- charToCount[character] += 1
244- }
245-
246- for (character in 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!
255177```
0 commit comments