Before we get on to solving the problem, let us understand it. The problem
- gives us a string build up of characters a-z
- wants us to count the occurrence of each character
- wants us to check if we can reduce 1 from any character’s count and make all of them equal
To do this, first we create an array of size 26 and store the count of each character in the index according to its ASCII value-97 to start ‘a’ from 0;
Then we check if there is a difference of more than 1 between the first two elements which would immediately make the string invalid. Otherwise, we check for elements that are unequal to both of them, meaning we increase the count of changes required (isValidCount).
Thus if this count goes up by more than one, we return “NO” as the string is invalid.
We could also add the specific difference between the two elements to isValidCount as well.

Test cases :
