Problem Description
Write a function canWriteRansomNote(note, magazine) that takes two strings, note and magazine. Your function should return true if the note can be constructed by cutting out letters from the magazine and false otherwise.
Each letter in the
magazinecan only be used once.The comparison is case-sensitive (this makes it a bit easier!).
Test Cases:
canWriteRansomNote("a", "b")should returnfalsecanWriteRansomNote("aa", "ab")should returnfalsecanWriteRansomNote("aa", "aab")should returntruecanWriteRansomNote("find me", "i need to find a new me")should returntrue
Think about the logic we just used. Which string should you build the 'Character Map' from? And which string should you check against the map?
💡 The 'Syntax Nudge'
You have all the tools you need! The logic is identical, but the order is important.
You'll need to build a frequency map (just like
charMap) of the letters you have available. Which string represents the letters you have?Then, you'll loop through the string you're trying to build and check against that map.
You'll use the same
for...ofloop and the same Bracket Notation (map[char]) to solve this.