Atmost K

4.5

13 votes
String Algorithms, Algorithms, Hashing Algorithm
Problem

You are given a string S of length N containing lowercase English letters and an integer K. You can rearrange the characters of the string in any order. After that you split the string into several parts. Find the minimum number of parts in which you can split S such that each part satisfy the condition below:

  • The frequency of every unique character in the part is at most K.

Input Format

  • The first line contains an integer T, which denotes the number of test cases. For every test case:
  • The first line contains two space-separated integers N and K.
  • The second line contains the string S.

Output Format

For each test case, print the minimum number of parts in which you can split S such that each part satisfy the condition.

Constraints

1T101KN105S contains lowercase English letters

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Test case 1: We can rearrange S as "dbdbd" and split into "dbd", "bd". Hence, the answer is 2.

Test case 2: We can rearrange S as "aaahus" and we do not have any need to split S. Hence, the answer is 1.

Editor Image

?