Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // K Unique String - KOTLIN
- import java.util.*;
- var sc = Scanner(System.`in`)
- fun main() {
- val s = sc.next()
- val k = sc.nextInt()
- println(kUniqueString(s, k))
- }
- fun kUniqueString(s: String, k: Int): Int {
- val freq = HashMap<Char, Int>()
- for (i in 0 until s.length) {
- freq[s[i]] = freq.getOrDefault(s[i], 0) + 1
- }
- val st: MutableSet<MyPair> =
- TreeSet { a, b ->
- if (a.frequency == b.frequency)
- a.ch - b.ch
- else
- a.frequency - b.frequency
- }
- for ((key, value) in freq) {
- st.add(MyPair(key, value))
- }
- var distinct = freq.size
- var changes = 0
- for (x in st) {
- if (distinct <= k) break
- changes += x.frequency
- distinct--
- }
- return changes
- }
- internal class MyPair(var ch: Char, var frequency: Int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement