forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxNumberOfBalloons.kt
54 lines (50 loc) · 1.56 KB
/
MaxNumberOfBalloons.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package questions
import kotlin.test.assertEquals
/**
* Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
* You can use each character in text at most once. Return the maximum number of instances that can be formed.
*
* Input: text = "nlaebolko"; Output: 1
*
* [Source](https://leetcode.com/explore/item/3973)
*/
fun maxNumberOfBalloons(text: String): Int {
if (text.length < "balloon".length)
return 0
val balloon = arrayOf(0, 0, 0, 0, 0, 0, 0)
// OR use when
val strategy: Map<Char, () -> Unit> = mapOf(
'b' to { balloon[0]++ },
'a' to { balloon[1]++ },
'n' to { balloon[6]++ },
'l' to {
if (balloon[2] == balloon[3]) {
balloon[2]++
} else if (balloon[2] < balloon[3]) {
balloon[2]++
} else {
balloon[3]++
}
},
'o' to {
if (balloon[4] == balloon[5]) {
balloon[4]++
} else if (balloon[4] < balloon[5]) {
balloon[4]++
} else {
balloon[5]++
}
}
)
text.forEach {
strategy[it]?.invoke()
}
return balloon.minByOrNull { it }!!
}
fun main() {
assertEquals(1, maxNumberOfBalloons("nlaebolko"))
assertEquals(2, maxNumberOfBalloons("loonbalxballpoon"))
assertEquals(0, maxNumberOfBalloons("leetcode"))
assertEquals(1, maxNumberOfBalloons("balloon"))
assertEquals(3, maxNumberOfBalloons("balloonballoonballoon"))
}