-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0383-ransom-note.js
More file actions
33 lines (27 loc) · 1008 Bytes
/
Copy path0383-ransom-note.js
File metadata and controls
33 lines (27 loc) · 1008 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
//Blog: https://www.allenliservice.online/leetcode-js-383-ransom-note/
// <strong>solution:</strong>
// 1. 將 magazine 中的字串轉成陣列放入 array。
// 2. 從 ransomNote中取出每一個字串
// 3. 宣告 index 為 array 中符合 element 的元素位置。
// 4. 如果不存在,indexOf 為 - 1,則 index 為負數跳出迴圈。
// 5. 如果存在,使用 splice 將該元素刪除。
// 6. 如 magazine 中階存在 ransomNote 的元素,則回傳 true。
// <strong>Code 1</strong>
var canConstruct = function (ransomNote, magazine) {
let array = [...magazine];
for (const element of ransomNote) {
const index = array.indexOf(element);
if (index < 0) return false;
array.splice(index, 1);
}
return true;
};
/* <strong>Example 3</strong>
<pre style='background-color:#ggg'>
Input: ransomNote = "aa", magazine = "aab"
index = array.indexOf(a) //0
array.splice(index, 1) //['a']
index = array.indexOf(a) //0
array.splice(index, 1) //['a']
return true;
</pre> */