Count Character Frequency
Difficulty: Easy · Topics: Map, String, Counting
Difficulty: Easy | Tags: String, Map, Counting
Description
Given a string s, return a map/dictionary where each key is a character and the value is the number of times it appears in the string.
Implement:
charFrequency(s) → Map<Character, Integer>
Examples
Example 1:
Input: "hello"
Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
Example 2:
Input: "aabbcc"
Output: {'a': 2, 'b': 2, 'c': 2}
Example 3:
Input: ""
Output: {}
Example 4:
Input: "AaBb"
Output: {'A': 1, 'a': 1, 'B': 1, 'b': 1}
Explanation: Case-sensitive by default.
Constraints
0 <= s.length <= 10^5scontains printable ASCII characters.- Counting is case-sensitive.
Follow-up
- Add a case-insensitive mode.
- Return the top K most frequent characters.