Created
May 3, 2020 23:12
-
-
Save munguial/ee0185f93280c1e3bc96ced23aeddf38 to your computer and use it in GitHub Desktop.
Day 3 - Ransom Note
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
| class Solution { | |
| fun canConstruct(ransomNote: String, magazine: String): Boolean { | |
| var m = HashMap<Char, Int>() | |
| magazine.forEach { | |
| if (m.containsKey(it)) { | |
| m[it] = m[it]!!.plus(1) | |
| } else { | |
| m[it] = 1 | |
| } | |
| } | |
| ransomNote.forEach { | |
| if (m.containsKey(it) && m.get(it)!! > 0) { | |
| m[it] = m[it]!!.minus(1) | |
| } else { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment