|
| 1 | +/** |
| 2 | + * Link: https://leetcode.com/problems/majority-frequency-characters/ |
| 3 | + * Title: Majority Frequency Characters |
| 4 | + * |
| 5 | + * Question: |
| 6 | + * You are given a string s consisting of lowercase English letters. |
| 7 | + * |
| 8 | + * The frequency group for a value k is the set of characters that appear exactly k times in s. |
| 9 | + * The majority frequency group is the group that contains the largest number of distinct characters. |
| 10 | + * If two or more groups tie for the largest size, choose the group with the larger frequency k. |
| 11 | + * |
| 12 | + * Return a string containing all characters in the majority frequency group, in any order. |
| 13 | + * |
| 14 | + * -------------------------------------------------------------------- |
| 15 | + * Approach: |
| 16 | + * - Step 1: Count frequencies of each character using a HashMap. |
| 17 | + * - Step 2: Build frequency groups: for each frequency k, store all characters that appear k times. |
| 18 | + * - Step 3: Iterate over all groups: |
| 19 | + * - Track the group with the maximum size (# of distinct characters). |
| 20 | + * - If there is a tie in size, pick the group with the larger frequency value. |
| 21 | + * - Step 4: Return the characters of the chosen group. |
| 22 | + * |
| 23 | + * -------------------------------------------------------------------- |
| 24 | + * Dry Run: |
| 25 | + * s = "aabbccc" |
| 26 | + * |
| 27 | + * Frequencies: |
| 28 | + * a -> 2 |
| 29 | + * b -> 2 |
| 30 | + * c -> 3 |
| 31 | + * |
| 32 | + * Groups: |
| 33 | + * freq=2 → "ab" |
| 34 | + * freq=3 → "c" |
| 35 | + * |
| 36 | + * Group sizes: |
| 37 | + * "ab" → size=2 |
| 38 | + * "c" → size=1 |
| 39 | + * |
| 40 | + * Best = "ab" (since size=2 > size=1) |
| 41 | + * Output = "ab" |
| 42 | + * |
| 43 | + * -------------------------------------------------------------------- |
| 44 | + * Time Complexity: O(n) |
| 45 | + * - One pass to count frequencies, one pass to build groups, |
| 46 | + * one pass to find the best group. |
| 47 | + * Space Complexity: O(n) |
| 48 | + * - Storing frequency map and groups. |
| 49 | + */ |
| 50 | + |
| 51 | +class Solution { |
| 52 | + public String majorityFrequencyGroup(String s) { |
| 53 | + // Step 1: frequency map |
| 54 | + HashMap<Character, Integer> fre = new HashMap<>(); |
| 55 | + for (char ch : s.toCharArray()) { |
| 56 | + fre.put(ch, fre.getOrDefault(ch, 0) + 1); |
| 57 | + } |
| 58 | + |
| 59 | + // Step 2: group by frequency |
| 60 | + HashMap<Integer, String> group = new HashMap<>(); |
| 61 | + for (Map.Entry<Character, Integer> pair : fre.entrySet()) { |
| 62 | + int val = pair.getValue(); |
| 63 | + char ch = pair.getKey(); |
| 64 | + group.put(val, group.getOrDefault(val, "") + ch); |
| 65 | + } |
| 66 | + |
| 67 | + // Step 3: choose best group |
| 68 | + int bestSize = 0; |
| 69 | + int bestFreq = 0; |
| 70 | + String ans = ""; |
| 71 | + |
| 72 | + for (Map.Entry<Integer, String> pair : group.entrySet()) { |
| 73 | + String chars = pair.getValue(); |
| 74 | + int freq = pair.getKey(); |
| 75 | + int size = chars.length(); |
| 76 | + |
| 77 | + if (size > bestSize || (size == bestSize && freq > bestFreq)) { |
| 78 | + bestSize = size; |
| 79 | + bestFreq = freq; |
| 80 | + ans = chars; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ans; |
| 85 | + } |
| 86 | +} |
0 commit comments