problem
sort
class Solution {
fun minDeletions(s: String): Int {
val freqs = Array<Int>(26) { 0 }
s.forEach { c ->
freqs[c.toInt() - 'a'.toInt()] += 1
}
freqs.sortBy { -it }
var prev = freqs[0]
var answer = 0
for (i in 1 until 26) {
if (freqs[i] == 0) break
if (freqs[i] < prev) {
prev = freqs[i]
continue
}
if (prev == 0) {
answer += freqs[i]
prev = 0
} else {
answer += (freqs[i] - prev + 1)
prev--
}
}
return answer
}
}
set
class Solution {
fun minDeletions(s: String): Int {
val freqs = Array<Int>(26) { 0 }
s.forEach { c ->
freqs[c - 'a'] += 1
}
val set = mutableSetOf<Int>()
var answer = 0
freqs.forEach { freq ->
var temp = freq
while (temp > 0 && !set.add(temp)) {
temp--
answer++
}
}
return answer
}
}