|
| 1 | +<h2><a href="https://leetcode.com/problems/sort-characters-by-frequency">Sort Characters By Frequency</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Given a string <code>s</code>, sort it in <strong>decreasing order</strong> based on the <strong>frequency</strong> of the characters. The <strong>frequency</strong> of a character is the number of times it appears in the string.</p> |
| 2 | + |
| 3 | +<p>Return <em>the sorted string</em>. If there are multiple answers, return <em>any of them</em>.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | + |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> s = "tree" |
| 10 | +<strong>Output:</strong> "eert" |
| 11 | +<strong>Explanation:</strong> 'e' appears twice while 'r' and 't' both appear once. |
| 12 | +So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong class="example">Example 2:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> s = "cccaaa" |
| 19 | +<strong>Output:</strong> "aaaccc" |
| 20 | +<strong>Explanation:</strong> Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers. |
| 21 | +Note that "cacaca" is incorrect, as the same characters must be together. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong class="example">Example 3:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> s = "Aabb" |
| 28 | +<strong>Output:</strong> "bbAa" |
| 29 | +<strong>Explanation:</strong> "bbaA" is also a valid answer, but "Aabb" is incorrect. |
| 30 | +Note that 'A' and 'a' are treated as two different characters. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p> </p> |
| 34 | +<p><strong>Constraints:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li> |
| 38 | + <li><code>s</code> consists of uppercase and lowercase English letters and digits.</li> |
| 39 | +</ul> |
0 commit comments