
Given S = "aaabbc" and N = 4.
Frequency of each character is 'a' = 3, 'b' = 2, and 'c' = 1.
If we take ‘STR’ = "aabc" and add this 2 times i.e "aabcaabc" the frequency of each character will be 'a' = 4, 'b' = 2, 'c' = 2. Here the frequency of each character is greater or equal to that of string ‘S’. i.e for 'a' 4 >= 3, 'b' 2 >= 2 and 'c' 2 >= 1.
Hence X = 2.
We can't find any other string 'STR' of length N such that X < 2.
The first line contains a single integer ‘T’ representing the number of test cases. Then test cases follow:
The first line contains an integer ‘N’ representing the maximum length of string that ninja can write.
The second line contains a string ‘S’ representing the given string.
For each test case, return an integer denoting the minimum possible value of 'X'.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
It is guaranteed that the solution will always exist.
1 <= T <= 10
1 <= N <= 10^5
1 <= |S| <= 10^5
where 'S' only contains lowercase letters i.e a-z.
The total number of distinct characters in 'S' is always less than equal to N.
Time Limit: 1 sec
In this approach, we will find the minimum possible value of ‘N’ given a fixed number of sheets.
Let ‘X’ denotes the minimum number of times we need to repeat ‘STR’.
If a character repeats ‘P’ times in the ‘S’ then it must appear ceil(P / X) times in ‘STR’, Thus we will get the minimum possible value of ‘N’ by summing the ceil value of each character.
Now we check for every value of ‘X’ from 1 to S.length()(we are taking S.length() as even in worst case i.e. all characters equal and ‘N’ = 1 maximum possible answer is S.length()) if that give less ‘N’ then this is the required one then we will return that value.
Algorithm:
Function for checking the minimum possible value of ‘N’ IS_POSS(MID, FREQ, N)
In this approach, we will just optimise the previous approach by apply binary search on the value of ‘X’ instead of checking all values.
Algorithm:
Function for checking the minimum possible value of ‘N’ IS_POSS(MID, FREQ, N)
Element Count in Ranges
First Digit One
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store