Created
November 17, 2019 23:36
-
-
Save thexande/d15b6595b6bdfcc81e350d1104e9f0c2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
final class RansomNoteBuilder { | |
func isValidRansomNote(note: String, in magazine: String) -> Bool { | |
var lookup = magazine.reduce(into: [Character:Int]()) { dictionary, letter in | |
dictionary[letter] = (dictionary[letter] ?? 0) + 1 | |
} | |
for character in note { | |
guard let count = lookup[character], count > 0 else { return false } | |
lookup[character] = count - 1 | |
} | |
return true | |
} | |
} | |
RansomNoteBuilder().isValidRansomNote(note: "aabs", in: "aaabdddees") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment