LeetCode solutions

4053. Majority Frequency Characters

My accepted Python solution to LeetCode problem 4053, Majority Frequency Characters, running in 3ms.

  • Difficulty: Easy
  • Python
  • Runtime 3ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.4MB, accepted 2025-12-31.

python
class Solution:
    def majorityFrequencyGroup(self, s: str) -> str:
        from collections import Counter
        
        # Count frequency of each character
        char_freq = Counter(s)
        
        # Group characters by frequency
        freq_groups = {}
        for char, freq in char_freq.items():
            if freq not in freq_groups:
                freq_groups[freq] = []
            freq_groups[freq].append(char)
        
        # Find the majority frequency group
        # (largest group size, if tie then larger frequency)
        best_freq = None
        best_size = 0
        for freq, chars in freq_groups.items():
            size = len(chars)
            if size > best_size or (size == best_size and (best_freq is None or freq > best_freq)):
                best_size = size
                best_freq = freq
        
        return ''.join(freq_groups[best_freq])

Source