You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h2><ahref="https://leetcode.com/problems/sort-characters-by-frequency/">451. Sort Characters By Frequency</a></h2><h3>Medium</h3><hr><div><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><strongclass="example">Example 1:</strong></p>
7
+
8
+
<pre><strong>Input:</strong> s = "tree"
9
+
<strong>Output:</strong> "eert"
10
+
<strong>Explanation:</strong> 'e' appears twice while 'r' and 't' both appear once.
11
+
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
12
+
</pre>
13
+
14
+
<p><strongclass="example">Example 2:</strong></p>
15
+
16
+
<pre><strong>Input:</strong> s = "cccaaa"
17
+
<strong>Output:</strong> "aaaccc"
18
+
<strong>Explanation:</strong> Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
19
+
Note that "cacaca" is incorrect, as the same characters must be together.
20
+
</pre>
21
+
22
+
<p><strongclass="example">Example 3:</strong></p>
23
+
24
+
<pre><strong>Input:</strong> s = "Aabb"
25
+
<strong>Output:</strong> "bbAa"
26
+
<strong>Explanation:</strong> "bbaA" is also a valid answer, but "Aabb" is incorrect.
27
+
Note that 'A' and 'a' are treated as two different characters.
0 commit comments