591.Count the Frequency of Each Character
Easy
Given a string
text, return how often each character occurs, as entries like "l:3", listed in the order the characters first appear. Spaces are not counted, and upper- and lowercase letters count separately.
This is frequency counting applied to characters. Rescanning the text for every new character works but costs O(n²); counting everything in one pass into a map is O(n). The extra requirement — report in first-appearance orderInsertion OrderThe order in which keys were first added to a map. Some maps remember it; others do not. — matters when choosing the map type.
Example 1:
Input: text = "hello world"
Output: ["h:1","e:1","l:3","o:2","w:1","r:1","d:1"]
Example 2:
Input: text = "aaa"
Output: ["a:3"]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ text.length ≤ 1000 - ●
text contains printable ASCII characters and at least one non-space character
text =
hello world