Skip to main content

Leetcode - 383. Ransom Note

· One min read

https://leetcode.com/problems/ransom-note/

Solution 1

/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function (ransomNote, magazine) {
const splitB = magazine.split('');

for (let i = 0; i < ransomNote.length; i++) {
const matchedIndex = splitB.indexOf(ransomNote[i]);
if (matchedIndex !== -1) {
splitB.splice(matchedIndex, 1);
} else {
return false;
}
}

return true;
};

Runtime: 116 ms, faster than 76.45% of JavaScript online submissions for Ransom Note.

Memory Usage: 46.5 MB, less than 30.65% of JavaScript online submissions for Ransom Note.