#C3194. Minimum Contiguous Substring Removal

    ID: 46594 Type: Default 1000ms 256MiB

Minimum Contiguous Substring Removal

Minimum Contiguous Substring Removal

You are given a string s of length n. Your task is to remove exactly one contiguous substring from s (possibly empty) so that in the remaining string every character that appears has the same frequency.

For example, consider the string "aabbccd" with n = 7. By removing the substring "d" (which is contiguous), the frequencies of 'a', 'b', and 'c' in the remaining string become 2, 2, and 2 respectively, which are all equal. Thus, the answer is 1, which is the length of the removed substring.

If the string already has uniform frequencies (or if it is possible by removing an empty substring), then the answer is 0.

Note: A removed substring is defined by two indices l and r (with 0 ≤ l ≤ r ≤ n) such that the substring s[l:r] is removed. The remaining string is the concatenation of s[0:l] and s[r:n].

inputFormat

The input consists of two lines. The first line contains a single integer n representing the length of the string s. The second line contains the string s, which consists of lowercase letters.

outputFormat

Output a single integer which is the minimum length of the contiguous substring that needs to be removed so that the frequencies of all characters present in the remaining string are equal. If it is not necessary to remove any substring, output 0.

## sample
7
aabbccd
1