Determine whether a word is valid according to a set of character-count rules.
Valid Word
Given a word, determine whether it is valid based on a frequency rule over its characters.
A word is considered valid if one of the following is true:
- Every character appears the same number of times, or
- It can become valid after removing exactly one occurrence of any single character.
Your task is to decide whether the given word is valid.
Notes
- Treat characters as case-sensitive unless stated otherwise.
- The exact characters allowed depend on the input format, but the logic should work for any string of lowercase letters or general ASCII characters.
- If the word is already valid, that also counts as valid.
Input Format
Input
A single string word.
Interpretation
- Count the frequency of each distinct character in
word. - Check whether the frequencies are all equal, or can be made equal by deleting one character occurrence.
Output Format
Output
Return true if the word is valid, otherwise return false.
Constraints
1 <= word.length <= $10^{5}$- The implementation should be efficient enough to handle large strings.
- Use or near- time where possible.
Example 1
Input
word = "aabbcc"
Output
true
Explanation
All characters appear exactly 2 times, so the word is valid.
Example 2
Input
word = "aabbccc"
Output
true
Explanation
Remove one 'c' to make the frequencies 2, 2, 2.
Show 2 more examples
Example 3
Input
word = "aabbc"
Output
true
Explanation
Remove the single 'c' and the remaining frequencies are 2 and 2.
Example 4
Input
word = "aabbcd"
Output
false
Explanation
No single removal can make all remaining character frequencies equal.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.