File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 383. Ransom Note
2
+ https://leetcode-cn.com/problems/ransom-note/
3
+ Given two strings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
4
+ Each letter in magazine can only be used once in ransomNote.
5
+
6
+ Example 1:
7
+ Input: ransomNote = "a", magazine = "b"
8
+ Output: false
9
+
10
+ Example 2:
11
+ Input: ransomNote = "aa", magazine = "ab"
12
+ Output: false
13
+
14
+ Example 3:
15
+ Input: ransomNote = "aa", magazine = "aab"
16
+ Output: true
17
+
18
+ Constraints:
19
+ 1 <= ransomNote.length, magazine.length <= 10^5
20
+ ransomNote and magazine consist of lowercase English letters.
21
+
22
+ ## hashtable
23
+ ``` python3
24
+ class Solution :
25
+ def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
26
+ note_dict= Counter(ransomNote)
27
+ maga_dict= Counter(magazine)
28
+ for k,v in note_dict.items():
29
+ if k not in maga_dict.keys() or maga_dict[k]< v:
30
+ return False
31
+ return True
32
+ ```
33
+
34
+ ## bucket
35
+ ``` python3
36
+ class Solution :
37
+ def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
38
+ note_dict= defaultdict(int )
39
+ for i in ransomNote:
40
+ note_dict[i]+= 1
41
+ maga_dict= defaultdict(int )
42
+ for i in magazine:
43
+ maga_dict[i]+= 1
44
+ for i in ransomNote:
45
+ if i not in maga_dict or note_dict[i]> maga_dict[i]:
46
+ return False
47
+ return True
48
+ ```
49
+
You can’t perform that action at this time.
0 commit comments