- Published on
Hard: Sherlock and valid string
- Moch Lutfi
Question: Given a string, Sherlock considers it valid if all the characters in the string occur the same number of time. However, a string is also valid if the frequencies are same after removing any one character.
Input: str = “aabbcd”
Input: str = “abcc”
Let's break down the problem statement.
Sherlock needs to verify if a given string is valid. A string is valid under 2 conditions:
- All characters of the string have same frequencies
- If you can delete any one character from the string to achieve condition #1.
- Init map then find out how many repetition of char
YESif string input consist of 1 char only such as "aaaa" or "bbb" or "zz"
- Init another map to find out how many char with
- Find the most frequencies occurred
- Iterate from the first map
- Check if the current frequency is different with
hasUniqFreqor different frequency more than 1, otherwise set
Try it yourself https://go.dev/play/p/jtD3sCn5LkJ