Character Frequency Counting in DSA C - Time & Space Complexity
We want to understand how long it takes to count how often each character appears in a string.
The question is: how does the time needed grow as the string gets longer?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
void countFrequency(char *str) {
int freq[256] = {0};
int i;
for(i = 0; str[i] != '\0'; i++) {
freq[(unsigned char)str[i]]++;
}
for(i = 0; i < 256; i++) {
if(freq[i] != 0) {
printf("%c: %d\n", i, freq[i]);
}
}
}
int main() {
char str[] = "hello world";
countFrequency(str);
return 0;
}
This code counts how many times each character appears in the input string and prints the counts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The first loop goes through each character in the string once.
- How many times: It runs once for every character in the input string (n times).
- The second loop runs 256 times, which is a fixed number, not depending on input size.
As the string gets longer, the first loop runs more times, directly matching the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times for the first loop + 256 for the second |
| 100 | About 100 times for the first loop + 256 for the second |
| 1000 | About 1000 times for the first loop + 256 for the second |
Pattern observation: The main work grows directly with the string length, while the second loop stays the same.
Time Complexity: O(n)
This means the time to count characters grows in a straight line with the string length.
[X] Wrong: "The second loop over 256 characters makes this O(n * 256) which is O(n²)."
[OK] Correct: The second loop runs a fixed 256 times, no matter how big the input is, so it does not multiply with n.
Counting character frequency efficiently is a common task that shows you understand loops and arrays well.
Knowing how time grows helps you explain your code clearly in interviews.
"What if we used a linked list to store frequencies instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand character frequency counting
It means counting how many times each character appears 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 -> Option AQuick Check:
Frequency counting = count of each character [OK]
- Confusing frequency with string length
- Thinking frequency means position
- Mixing frequency with first character
Solution
Step 1: Understand frequency array declaration
We need an integer array of size 256 initialized to zero to count ASCII characters.Step 2: Check each option
int freq[256] = {0}; declares int freq[256] = {0}; which is correct. Others either declare wrong types or wrong initialization.Final Answer:
int freq[256] = {0}; -> Option AQuick Check:
Array of 256 ints initialized to zero = int freq[256] = {0}; [OK]
- Using char array instead of int
- Not initializing array
- Wrong array size or syntax
"aabbc" using a frequency array indexed by character ASCII values?Solution
Step 1: Count characters in the string "aabbc"
Characters: 'a' appears 2 times, 'b' appears 2 times, 'c' appears 1 time.Step 2: Match counts with options
{"a": 2, "b": 2, "c": 1} matches the counts exactly. Others have incorrect counts.Final Answer:
{"a": 2, "b": 2, "c": 1} -> Option CQuick Check:
Count 'a'=2, 'b'=2, 'c'=1 [OK]
- Mixing counts of 'b' and 'c'
- Counting characters incorrectly
- Ignoring character case
char str[] = "hello"; int freq[256]; for(int i = 0; i < 256; i++) freq[i] = 0; for(int i = 0; i <= strlen(str); i++) freq[str[i]]++;
Solution
Step 1: Analyze loop condition for frequency counting
The loop runs from i = 0 to i <= strlen(str), which includes the null character '\0' at the end.Step 2: Understand why this causes error
Accessing freq[str[i]] when str[i] is '\0' is valid but incrementing frequency for null char is usually unintended. Loop should run till i < strlen(str) to avoid counting null.Final Answer:
Loop should run till i < strlen(str), not <= strlen(str) -> Option BQuick Check:
Loop boundary off by one error [OK]
- Using <= instead of <
- Wrong array size assumption
- Wrong data type for freq array
Solution
Step 1: Understand ignoring case means treating 'A' and 'a' same
To do this, convert each character to lowercase (or uppercase) before counting frequency.Step 2: Check options for correct approach
Convert each character to lowercase before counting frequency converts characters to lowercase before counting, which correctly ignores case. Others either separate counts or ignore characters incorrectly.Final Answer:
Convert each character to lowercase before counting frequency -> Option DQuick Check:
Case normalization before counting = Convert each character to lowercase before counting frequency [OK]
- Counting uppercase and lowercase separately
- Ignoring uppercase letters
- Using wrong array size
Ready to prove it?
Pass the test and this pattern moves unknown
