Class Solution
java.lang.Object
g0401_0500.s0451_sort_characters_by_frequency.Solution
451 - Sort Characters By Frequency.<p>Medium</p>
<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>
<p>Return <em>the sorted string</em>. If there are multiple answers, return <em>any of them</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> s = “tree”</p>
<p><strong>Output:</strong> “eert”</p>
<p><strong>Explanation:</strong> ‘e’ appears twice while ‘r’ and ‘t’ both appear once. So ‘e’ must appear before both ‘r’ and ‘t’. Therefore “eetr” is also a valid answer.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> s = “cccaaa”</p>
<p><strong>Output:</strong> “aaaccc”</p>
<p><strong>Explanation:</strong> Both ‘c’ and ‘a’ appear three times, so both “cccaaa” and “aaaccc” are valid answers. Note that “cacaca” is incorrect, as the same characters must be together.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> s = “Aabb”</p>
<p><strong>Output:</strong> “bbAa”</p>
<p><strong>Explanation:</strong> “bbaA” is also a valid answer, but “Aabb” is incorrect. Note that ‘A’ and ‘a’ are treated as two different characters.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
<li><code>s</code> consists of uppercase and lowercase English letters and digits.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
frequencySort
-