To check if frequencies of all characters of a string can become the same by just one removal, we can use Python's Counter
from the collections
module to calculate the frequencies of each character, and then we analyze the frequency counts.
Here's a step-by-step approach:
Counter
.True
(all frequencies are already the same).False
.Here is the Python function that does this:
from collections import Counter def can_frequencies_become_same(s): freq = Counter(s) freq_counts = Counter(freq.values()) if len(freq_counts) == 1: # All characters already have the same frequency return True elif len(freq_counts) == 2: # Check for the case where one removal can make frequencies the same keys = list(freq_counts.keys()) if 1 in freq_counts.values() and (abs(keys[0] - keys[1]) == 1 or min(keys) == 1): return True return False # Examples print(can_frequencies_become_same('xxxyyzz')) # Should return True print(can_frequencies_become_same('xxxxyyzz')) # Should return False
Explanation of the key part:
True
.The Counter
objects make it easy to implement this logic succinctly and efficiently.
azure-storage-files m mp4 contextmanager bootstrap-vue appium-android emulation alter-table sublist in-app-purchase