DSA problem
Count Occurrence of Each Character
Given a string s, compute the frequency of each character in it.
This is the string equivalent of array frequency counting. Iterate through each character and build a frequency map (or an array of size 26 for lowercase-only strings, which is slightly faster), preserving the order in which characters first appear. This teaches character-level processing and frequency analysis - the backbone of text analytics, compression algorithms like Huffman coding, and natural language processing. In interviews it often appears as a warm-up before harder problems like minimum-window-substring or find-all-anagrams-in-a-string.
Examples
Example 1
Input:
s = "programming"Output:
p:1, r:2, o:1, g:2, a:1, m:2, i:1, n:1Explanation: Each distinct character is mapped to how many times it appears, in first-seen order.
Constraints
- 1 <= s.length <= 10^5
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.