fork download
  1. fun getMaxBurles(n: Int, k: Int, s: String): Int
  2. {
  3. val low = IntArray(26)
  4. val up = IntArray(26)
  5.  
  6. for (c in s) {
  7. if (c in 'a'..'z') {
  8. low[c - 'a']++
  9. } else {
  10. up[c - 'A']++
  11. }
  12. }
  13.  
  14. var pairs = 0
  15. var operation = 0
  16.  
  17.  
  18. for (i in 0 until 26)
  19. {
  20. val minCount = minOf(low[i], up[i])
  21. pairs += minCount
  22. operation += Math.abs(low[i] - up[i]) / 2
  23. }
  24.  
  25. val addition = minOf(k, operation)
  26. return pairs + addition
  27. }
  28.  
  29. fun main()
  30. {
  31. val t = readLine()!!.toInt()
  32.  
  33. val results = mutableListOf<Int>()
  34.  
  35. for (i in 0 until t)
  36. {
  37. val (n, k) = readLine()!!.split(" ").map { it.toInt() }
  38. val s = readLine()!!
  39. results.add(getMaxBurles(n, k, s))
  40. }
  41.  
  42. for (result in results)
  43. {
  44. println(result)
  45. }
  46. }
  47.  
Success #stdin #stdout 0.15s 42824KB
stdin
5
11 2
aAaaBACacbE
2 2
ab
4 1
aaBB
6 0
abBAcC
5 3
cbccb
stdout
5
0
1
3
2