mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-12 08:55:14 +08:00
134 lines
4.7 KiB
HTML
134 lines
4.7 KiB
HTML
<p>You are given a string <code>s</code> consisting of lowercase English letters.</p>
|
|
|
|
<p>The <strong>frequency group</strong> for a value <code>k</code> is the set of characters that appear exactly <code>k</code> times in s.</p>
|
|
|
|
<p>The <strong>majority frequency group</strong> is the frequency group that contains the largest number of <strong>distinct</strong> characters.</p>
|
|
|
|
<p>Return a string containing all characters in the majority frequency group, in <strong>any</strong> order. If two or more frequency groups tie for that largest size, pick the group whose frequency <code>k</code> is <strong>larger</strong>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "aaabbbccdddde"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"ab"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th style="border: 1px solid black;">Frequency (k)</th>
|
|
<th style="border: 1px solid black;">Distinct characters in group</th>
|
|
<th style="border: 1px solid black;">Group size</th>
|
|
<th style="border: 1px solid black;">Majority?</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td style="border: 1px solid black;">4</td>
|
|
<td style="border: 1px solid black;">{d}</td>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">No</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">3</td>
|
|
<td style="border: 1px solid black;">{a, b}</td>
|
|
<td style="border: 1px solid black;">2</td>
|
|
<td style="border: 1px solid black;"><strong>Yes</strong></td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">2</td>
|
|
<td style="border: 1px solid black;">{c}</td>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">No</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">{e}</td>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">No</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Both characters <code>'a'</code> and <code>'b'</code> share the same frequency 3, they are in the majority frequency group. <code>"ba"</code> is also a valid answer.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "abcd"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"abcd"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th style="border: 1px solid black;">Frequency (k)</th>
|
|
<th style="border: 1px solid black;">Distinct characters in group</th>
|
|
<th style="border: 1px solid black;">Group size</th>
|
|
<th style="border: 1px solid black;">Majority?</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">{a, b, c, d}</td>
|
|
<td style="border: 1px solid black;">4</td>
|
|
<td style="border: 1px solid black;"><strong>Yes</strong></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>All characters share the same frequency 1, they are all in the majority frequency group.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "pfpfgi"</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">"fp"</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th style="border: 1px solid black;">Frequency (k)</th>
|
|
<th style="border: 1px solid black;">Distinct characters in group</th>
|
|
<th style="border: 1px solid black;">Group size</th>
|
|
<th style="border: 1px solid black;">Majority?</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td style="border: 1px solid black;">2</td>
|
|
<td style="border: 1px solid black;">{p, f}</td>
|
|
<td style="border: 1px solid black;">2</td>
|
|
<td style="border: 1px solid black;"><strong>Yes</strong></td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">1</td>
|
|
<td style="border: 1px solid black;">{g, i}</td>
|
|
<td style="border: 1px solid black;">2</td>
|
|
<td style="border: 1px solid black;">No (tied size, lower frequency)</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Both characters <code>'p'</code> and <code>'f'</code> share the same frequency 2, they are in the majority frequency group. There is a tie in group size with frequency 1, but we pick the higher frequency: 2.</p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= s.length <= 100</code></li>
|
|
<li><code>s</code> consists only of lowercase English letters.</li>
|
|
</ul>
|