-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathransomNote.js
More file actions
33 lines (32 loc) · 1018 Bytes
/
ransomNote.js
File metadata and controls
33 lines (32 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
////////////////////////////////////////////////Ransom Note///////////////////////////////////////////
// Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
// Each letter in magazine can only be used once in ransomNote.
// Example 1:
//
// Input: ransomNote = "a", magazine = "b"
// Output: false
// Example 2:
//
// Input: ransomNote = "aa", magazine = "ab"
// Output: false
// Example 3:
//
// Input: ransomNote = "aa", magazine = "aab"
// Output: true
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
const canConstruct = function(ransomNote, magazine) {
let m = magazine.split('');
for (let i = 0; i < ransomNote.length; i++) {
const index = m.indexOf(ransomNote[i]);
if (index === -1) return false;
m.splice(index, 1);
}
return true;
};
// console.log(canConstruct("a", "b"));
// console.log(canConstruct("aa", "ab"));
// console.log(canConstruct("aa", "aab"));