A trail-mix jar holds n pieces, and every piece is one of 26 ingredient types, written as a lowercase English letter. A picnicker wants to eat exactly one single piece from the jar (any one occurrence of any type) so that, among the pieces that remain, every ingredient type still present appears exactly the same number of times as every other type still present (a jar left with only one ingredient type overall, or completely empty, counts as balanced). Given the jar's contents, determine whether such a piece to eat exists.
Line 1: an integer n, the number of pieces in the jar. Line 2: a string word of length n consisting of lowercase English letters, the jar's contents (the order of the letters does not affect the answer).
Print "YES" if some single piece can be eaten to balance the remaining ingredient-type frequencies, or "NO" otherwise.
Example 1
Input
5 aabbc
Expected
YES
Explanation
The jar is "aabbc" with frequencies a=2, b=2, c=1. Eating the single 'c' piece removes that type entirely, leaving a=2 and b=2 -- every remaining type appears exactly twice, so the answer is "YES".
Example 2
Input
6 abccba
Expected
NO
Explanation
The jar is "abccba" with frequencies a=2, b=2, c=2 -- already perfectly balanced. But eating any one piece drops that type's count to 1 while the other two types stay at 2, and the eaten type still has 1 piece left (it does not vanish), giving frequencies {2, 2, 1}, which are not all equal. No single removal works, so the answer is "NO".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →