Character Frequency Counter - Problem
Given a string, find the frequency of each character and display the results in descending order of frequency.
If two characters have the same frequency, they can appear in any order relative to each other.
Input: A single string containing letters, numbers, and special characters
Output: A string showing each character and its frequency, sorted by frequency in descending order
Format: Each character-frequency pair should be displayed as "character:frequency", separated by commas
Input & Output
Example 1 — Basic Case
$
Input:
s = "hello"
›
Output:
l:2,h:1,e:1,o:1
💡 Note:
Character frequencies: h→1, e→1, l→2, o→1. Sorted by frequency descending: l appears 2 times (highest), then h, e, o each appear 1 time
Example 2 — Mixed Characters
$
Input:
s = "aabbcc"
›
Output:
a:2,b:2,c:2
💡 Note:
All characters have same frequency (2), so they can appear in any order relative to each other
Example 3 — Single Character
$
Input:
s = "aaaa"
›
Output:
a:4
💡 Note:
Only one unique character 'a' appears 4 times
Constraints
- 1 ≤ s.length ≤ 1000
- s contains only printable ASCII characters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code