[Solution] Diverse Substrings Codeforces Solution
A non-empty digit string is diverse if the number of occurrences of each character in it doesn't exceed the number of distinct characters in it.
For example:
- string "7" is diverse because 7 appears in it time and the number of distinct characters in it is ;
- string "77" is not diverse because 7 appears in it times and the number of distinct characters in it is ;
- string "1010" is diverse because both 0 and 1 appear in it times and the number of distinct characters in it is ;
- string "6668" is not diverse because 6 appears in it times and the number of distinct characters in it is .
You are given a string of length , consisting of only digits to . Find how many of its substrings are diverse.
A string is a substring of a string if can be obtained from by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Note that if the same diverse string appears in multiple times, each occurrence should be counted independently. For example, there are two diverse substrings in "77" both equal to "7", so the answer for the string "77" is .
Each test contains multiple test cases. The first line contains a single integer () — the number of test cases.
The first line of each test case contains a single integer () — the length of the string .
The second line of each test case contains a string of length . It is guaranteed that all characters of are digits from to .
It is guaranteed that the sum of over all test cases does not exceed .
For each test case print one integer — the number of diverse substrings of the given string .
In the first test case, the diverse substring is "7".
In the second test case, the only diverse substring is "7", which appears twice, so the answer is .
In the third test case, the diverse substrings are "0" ( times), "01", "010", "1" ( times), "10" ( times), "101" and "1010".
In the fourth test case, the diverse substrings are "0" ( times), "01", "011", "0110", "1" ( times), "10", "100", "110" and "1100".
In the fifth test case, the diverse substrings are "3", "39", "399", "6", "9" ( times), "96" and "996".
In the sixth test case, all non-empty substrings of "23456" are diverse.
No comments:
Post a Comment