Character Frequency Counting in DSA Go - Time & Space Complexity
Counting how often each character appears in a string is a common task in programming.
We want to know how the time needed grows as the string gets longer.
Analyze the time complexity of the following code snippet.
func charFrequency(s string) map[rune]int {
freq := make(map[rune]int)
for _, ch := range s {
freq[ch]++
}
return freq
}
This code counts how many times each character appears in the input string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character in the string once.
- How many times: Exactly once for each character in the string (n times).
As the string gets longer, the number of steps grows directly with its length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: Doubling the string length roughly doubles the work done.
Time Complexity: O(n)
This means the time to count characters grows in a straight line with the string length.
[X] Wrong: "Counting characters takes more time because of the map updates inside the loop."
[OK] Correct: Each map update is a quick operation, so the main time is just looping through the string once.
Understanding this helps you explain how simple loops and data structures work together efficiently.
"What if we used a nested loop to compare each character with every other character? How would the time complexity change?"
Practice
Solution
Step 1: Understand character frequency counting
It means counting how many times each character shows up in a string.Step 2: Compare with other options
Length, first character, or position are different concepts, not frequency counting.Final Answer:
How many times each character appears in the string -> Option AQuick Check:
Frequency counting = count characters [OK]
- Confusing frequency with string length
- Thinking frequency means position
- Mixing frequency with substring extraction
Solution
Step 1: Identify correct map initialization for character counts
We need a map with keys as characters (rune) and values as counts (int), so make(map[rune]int) is correct.Step 2: Check other options
freq := map[int]string{} uses wrong types (int keys, string values), C and D are slices, not maps.Final Answer:
freq := make(map[rune]int) -> Option AQuick Check:
Map for rune to int = freq map [OK]
- Using slice instead of map
- Wrong key or value types in map
- Not using make() for map initialization
package main
import "fmt"
func main() {
s := "hello"
freq := make(map[rune]int)
for _, ch := range s {
freq[ch]++
}
fmt.Println(freq['l'])
}Solution
Step 1: Count occurrences of 'l' in "hello"
The string "hello" has 'l' twice (positions 2 and 3).Step 2: Check map frequency for 'l'
freq['l'] increments twice, so freq['l'] = 2.Final Answer:
2 -> Option CQuick Check:
Count of 'l' in "hello" = 2 [OK]
- Counting only once
- Confusing index with count
- Using wrong character in print
package main
import "fmt"
func main() {
s := "test"
freq := map[rune]int{}
for i := 0; i < len(s); i++ {
freq[s[i]] += 1
}
fmt.Println(freq)
}Solution
Step 1: Check map key type and s[i] type
Map keys are rune but s[i] is byte, causing type mismatch.Step 2: Understand why this is an error
Go does not automatically convert byte to rune for map keys, so this causes a compile error.Final Answer:
Using s[i] as byte instead of rune causes wrong map key type -> Option DQuick Check:
Byte vs rune key mismatch error [OK]
- Ignoring byte vs rune difference
- Assuming map auto-converts keys
- Thinking map initialization is wrong
Solution
Step 1: Count character frequencies using a map
Use a map[rune]int to count how many times each character appears.Step 2: Iterate the map to find the character with the highest count
Loop through the map keys and values, track the max count and corresponding character.Final Answer:
Count frequencies in a map, then iterate map to find max count character -> Option BQuick Check:
Map count + max search = highest frequency char [OK]
- Sorting string does not give frequency
- Using slice without mapping chars
- Confusing length with frequency
Ready to prove it?
Pass the test and this pattern moves unknown
