forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRansomNote.kt
36 lines (33 loc) · 1.06 KB
/
RansomNote.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
* Each letter in magazine can only be used once in ransomNote.
*
* [Source](https://leetcode.com/problems/ransom-note-kt/)
*/
@UseCommentAsDocumentation
private fun canConstruct(ransomNote: String, magazine: String): Boolean {
val charCount = mutableMapOf<Char, Int>()
for (i in ransomNote) {
charCount[i] = charCount.getOrDefault(i, 0) + 1
}
for (i in magazine) {
val counts = charCount[i] ?: continue
if (counts - 1 == 0) {
charCount.remove(i)
} else {
charCount[i] = counts - 1
}
if (charCount.isEmpty()) {
return true
}
}
return false
}
fun main() {
canConstruct(ransomNote = "a", magazine = "b") shouldBe false
canConstruct(ransomNote = "aa", magazine = "ab") shouldBe false
canConstruct(ransomNote = "aa", magazine = "aab") shouldBe true
}