Created
December 31, 2020 17:02
-
-
Save ahmet-cetinkaya/c9e79784c150c932b647e1dec90a2154 to your computer and use it in GitHub Desktop.
LeetCode Solutions - 383. Ransom Note
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canConstruct = (ransomNote, magazine) => { | |
const dicMap = new Map(); | |
for (const c of magazine) dicMap.set(c, ~~dicMap.get(c) + 1); | |
for (const c of ransomNote) { | |
if (!dicMap.get(c)) return false; | |
dicMap.set(c, ~~dicMap.get(c) - 1); | |
} | |
return true; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canConstruct = (ransomNote, magazine) => { | |
let dicArr = [...magazine]; | |
for (const c of ransomNote) { | |
const index = dicArr.indexOf(c); | |
if (index < 0) return false; | |
dicArr.splice(index, 1); | |
} | |
return true; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const canConstruct = (ransomNote, magazine) => { | |
let dicObj = {}; | |
for (let i = 0; i < magazine.length; ++i) { | |
if (dicObj[magazine[i]]) ++dicObj[magazine[i]]; | |
else dicObj[magazine[i]] = 1; | |
} | |
for (let i = 0; i < ransomNote.length; ++i) { | |
if (dicObj[ransomNote[i]] === undefined || dicObj[ransomNote[i]] <= 0) return false; | |
--dicObj[ransomNote[i]]; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ransomNote.js
ransomNote2.js
ransomNote3.js