> For the complete documentation index, see [llms.txt](https://ugopireddy.gitbook.io/leet-code-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ugopireddy.gitbook.io/leet-code-solutions/solutions-451-500/451-sort-characters-by-frequency.md).

# 451 Sort Characters By Frequency

### Problem:

Given a string, sort it in decreasing order based on the frequency of characters.

Example 1:

```
Input:
"tree"

Output:
"eert"

Explanation:
'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.
```

Example 2:

```
Input:
"cccaaa"

Output:
"cccaaa"

Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
```

Example 3:

```
Input:
"Aabb"

Output:
"bbAa"

Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
```

### Solutions:

```java
public class Solution {
    public String frequencySort(String s) {
        HashMap<Character, Integer> appr = new HashMap<Character, Integer>();
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < s.length(); i ++) {
            char c = s.charAt(i);
            if (!appr.containsKey(c)) {
                appr.put(c, 0);
            }
            appr.put(c, appr.get(c) + 1);
            max = Math.max(max, appr.get(c));
        }
        HashMap<Integer, Queue<Character>> index = new HashMap<Integer, Queue<Character>>();
        for (Character c:appr.keySet()) {
            int count = appr.get(c);
            if (!index.containsKey(count)) {
                index.put(count, new LinkedList<Character>());
            }
            index.get(count).add(c);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = max; i > 0; i --) {
            if (index.containsKey(i)) {
                while (!index.get(i).isEmpty()) {
                    char c = index.get(i).poll();
                    for (int j = 0; j < i; j ++) {
                        sb.append(c);
                    }    
                }
            }
            
        }
        return sb.toString();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ugopireddy.gitbook.io/leet-code-solutions/solutions-451-500/451-sort-characters-by-frequency.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
