File tree 2 files changed +33
-0
lines changed 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 52
52
347|[ Top K Frequent Elements] ( ./0347-top-k-frequent-elements.js ) |Medium|
53
53
349|[ Intersection of Two Arrays] ( ./0349-intersection-of-two-arrays.js ) |Easy|
54
54
350|[ Intersection of Two Arrays II] ( ./0350-intersection-of-two-arrays-ii.js ) |Easy|
55
+ 383|[ Ransom Note] ( ./0383-ransom-note.js ) |Easy|
55
56
387|[ First Unique Character in a String] ( ./0387-first-unique-character-in-a-string.js ) |Easy|
56
57
448|[ Find All Numbers Disappeared in an Array] ( ./0448-find-all-numbers-disappeared-in-an-array.js ) |Easy|
57
58
451|[ Sort Characters By Frequency] ( ./0451-sort-characters-by-frequency.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 383. Ransom Note
3
+ * https://leetcode.com/problems/ransom-note/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given two stings ransomNote and magazine, return true if ransomNote
7
+ * can be constructed from magazine and false otherwise.
8
+ *
9
+ * Each letter in magazine can only be used once in ransomNote.
10
+ */
11
+
12
+ /**
13
+ * @param {string } ransomNote
14
+ * @param {string } magazine
15
+ * @return {boolean }
16
+ */
17
+ var canConstruct = function ( ransomNote , magazine ) {
18
+ const map = new Map ( ) ;
19
+
20
+ for ( let i = magazine . length - 1 ; i > - 1 ; i -- ) {
21
+ map . set ( magazine [ i ] , ( map . get ( magazine [ i ] ) || 0 ) + 1 ) ;
22
+ }
23
+
24
+ for ( let i = ransomNote . length - 1 ; i > - 1 ; i -- ) {
25
+ if ( ( map . get ( ransomNote [ i ] ) || 0 ) < 1 ) {
26
+ return false ;
27
+ }
28
+ map . set ( ransomNote [ i ] , map . get ( ransomNote [ i ] ) - 1 ) ;
29
+ }
30
+
31
+ return true ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments