451. Sort Characters By Frequency Medium
1/**
2 * [451] Sort Characters By Frequency
3 *
4 * Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
5 * Return the sorted string. If there are multiple answers, return any of them.
6 *
7 * Example 1:
8 *
9 * Input: s = "tree"
10 * Output: "eert"
11 * Explanation: '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 *
14 * Example 2:
15 *
16 * Input: s = "cccaaa"
17 * Output: "aaaccc"
18 * Explanation: 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 *
21 * Example 3:
22 *
23 * Input: s = "Aabb"
24 * Output: "bbAa"
25 * Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
26 * Note that 'A' and 'a' are treated as two different characters.
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= s.length <= 5 * 10^5
32 * s consists of uppercase and lowercase English letters and digits.
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/sort-characters-by-frequency/
38// discuss: https://leetcode.com/problems/sort-characters-by-frequency/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn frequency_sort(s: String) -> String {
44 String::new()
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_451() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.