Suppose we have a lowercase string s. We have to check whether the frequency of all characters are same after deleting one character or not.
So, if the input is like s = "abbc", then the output will be True as we can delete one b to get string "abc" where frequency of each element is 1.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import defaultdict def allSame(occurrence): counts = list(occurrence.values()) return all(element == counts[0] for element in counts) def solve(s): occurrence = defaultdict(int) for char in s: occurrence[char] += 1 if allSame(occurrence): return True for char in s: occurrence[char] -= 1 if allSame(occurrence): return True occurrence[char] += 1 return False s = "abbc" print(solve(s))
"abbc"
True