Skip to content

Instantly share code, notes, and snippets.

@thexande
Created November 17, 2019 23:36
Show Gist options
  • Save thexande/d15b6595b6bdfcc81e350d1104e9f0c2 to your computer and use it in GitHub Desktop.
Save thexande/d15b6595b6bdfcc81e350d1104e9f0c2 to your computer and use it in GitHub Desktop.
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