mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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> </p>
 | 
						|
<p><strong>Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "tree"
 | 
						|
<strong>Output:</strong> "eert"
 | 
						|
<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.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 2:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "cccaaa"
 | 
						|
<strong>Output:</strong> "aaaccc"
 | 
						|
<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.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p><strong>Example 3:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> s = "Aabb"
 | 
						|
<strong>Output:</strong> "bbAa"
 | 
						|
<strong>Explanation:</strong> "bbaA" is also a valid answer, but "Aabb" is incorrect.
 | 
						|
Note that 'A' and 'a' are treated as two different characters.
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </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>
 |