Check if frequency of all characters can become same by one removal in Python

Sometimes we need to check if removing exactly one character from a string can make all remaining characters have equal frequencies. This problem tests our understanding of frequency counting and pattern matching.

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 character is 1.

Approach

To solve this, we will follow these steps ?

  • Count frequency of all characters in the string
  • Check if frequencies are already equal (removing any character would work)
  • Try removing each character one by one and check if remaining frequencies become equal
  • Return True if any removal makes frequencies equal, otherwise False

Example

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)
    
    # Count frequency of each character
    for char in s:
        occurrence[char] += 1
    
    # If already all frequencies are same
    if allSame(occurrence):
        return True
    
    # Try removing each character one by one
    for char in s:
        occurrence[char] -= 1
        if allSame(occurrence):
            return True
        occurrence[char] += 1
    
    return False

# Test the function
s = "abbc"
print(solve(s))
True

How It Works

The algorithm works in two phases ?

  1. Frequency Counting: Create a dictionary to store character frequencies
  2. Pattern Testing: For each character position, temporarily reduce its frequency by 1 and check if all remaining frequencies become equal

Additional Example

Let's test with another string to see different scenarios ?

def solve(s):
    from collections import defaultdict
    
    occurrence = defaultdict(int)
    
    # Count frequency of each character
    for char in s:
        occurrence[char] += 1
    
    def allSame(occ):
        counts = list(occ.values())
        return all(element == counts[0] for element in counts)
    
    # If already all frequencies are same
    if allSame(occurrence):
        return True
    
    # Try removing each character one by one
    for char in s:
        occurrence[char] -= 1
        if allSame(occurrence):
            return True
        occurrence[char] += 1
    
    return False

# Test with different examples
test_cases = ["abbc", "aabbcc", "abc", "aaab"]

for test in test_cases:
    result = solve(test)
    print(f"String: '{test}' ? Can make equal frequencies: {result}")
String: 'abbc' ? Can make equal frequencies: True
String: 'aabbcc' ? Can make equal frequencies: True
String: 'abc' ? Can make equal frequencies: True
String: 'aaab' ? Can make equal frequencies: True

Time Complexity

The time complexity is O(n²) where n is the length of the string, since we iterate through each character and for each iteration, we check if all frequencies are equal.

Conclusion

This approach efficiently checks if removing one character can equalize all character frequencies. The key insight is to temporarily reduce each character's count and test the resulting frequency distribution.

Updated on: 2026-03-25T15:01:50+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements