https://leetcode.com/problems/maximum-number-of-balloons/ | Easy |
---|
Дана строка text
. Нужно определить, сколько раз можно составить слово "balloon"
, используя буквы из text
.
Input:
"nlaebolko”Output:
1
Input:
"loonbalxballpoon”Output:
2
Input:
"leetcode”Output:
0
fun maxNumberOfBalloons(text: String): Int {
val counts = IntArray(26)
for (char in text) counts[char - 'a']++
return minOf(
counts['b' - 'a'],
counts['a' - 'a'],
counts['l' - 'a'] / 2,
counts['o' - 'a'] / 2,
counts['n' - 'a']
)
}
O(n), где n
— длина строки text
.
O(1), так как используется фиксированный массив из 26 элементов.