First Non Repeating Character Using Hash in DSA C - Time & Space Complexity
We want to know how the time to find the first non-repeating character changes as the input string gets longer.
How does the number of steps grow when the string size increases?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
#include <string.h>
char firstNonRepeatingChar(char *str) {
int count[256] = {0};
int i;
int len = strlen(str);
for (i = 0; i < len; i++) {
count[(unsigned char)str[i]]++;
}
for (i = 0; i < len; i++) {
if (count[(unsigned char)str[i]] == 1) {
return str[i];
}
}
return '\0';
}
This code counts how many times each character appears, then finds the first character that appears only once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two loops over the input string.
- How many times: Each loop runs once over all characters, so twice over the string length.
As the string gets longer, the number of steps grows roughly twice as fast because we scan the string two times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The operations grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time to find the first non-repeating character grows directly in proportion to the string length.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply.
Understanding how to analyze simple loops like this helps you explain your code clearly and shows you know how to reason about efficiency.
"What if we used a nested loop to check each character against all others? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of hash in counting
A hash (dictionary) stores characters as keys and their counts as values, allowing quick frequency counting.Step 2: Identify why frequency counting helps
Knowing how many times each character appears helps find the first character with count 1, which is the first non-repeating character.Final Answer:
To count the frequency of each character efficiently -> Option DQuick Check:
Hash counts characters = frequency count [OK]
- Thinking hash sorts characters
- Assuming hash reverses string
- Using hash to remove duplicates only
Solution
Step 1: Check correct array initialization
In C, to count ASCII chars, an int array of size 256 initialized to zero is needed: int count[256] = {0};Step 2: Identify incorrect options
int count[] = NULL; sets pointer to NULL, no array. int count[256]; // uninitialized is uninitialized, may contain garbage. int count = 256; is a single int, not array.Final Answer:
int count[256] = {0}; -> Option AQuick Check:
Array size 256 with zero init = correct hash map [OK]
- Not initializing array to zero
- Using pointer instead of array
- Declaring single int instead of array
char str[] = "swiss";
int count[256] = {0};
for (int i = 0; str[i] != '\0'; i++) {
count[(int)str[i]]++;
}
for (int i = 0; str[i] != '\0'; i++) {
if (count[(int)str[i]] == 1) {
printf("%c", str[i]);
break;
}
}Solution
Step 1: Count character frequencies in "swiss"
Characters: s(3 times), w(1 time), i(1 time)Step 2: Find first character with count 1 in order
Order: s (count 3), w (count 1) -> first non-repeating is 'w'Final Answer:
w -> Option CQuick Check:
First unique char in "swiss" = w [OK]
- Printing first char without checking count
- Confusing 'i' and 'w' counts
- Ignoring character order
char str[] = "hello";
int count[256];
for (int i = 0; str[i] != '\0'; i++) {
count[(int)str[i]]++;
}
for (int i = 0; str[i] != '\0'; i++) {
if (count[(int)str[i]] == 1) {
printf("%c", str[i]);
break;
}
}Solution
Step 1: Check count array initialization
count array is declared but not initialized, so it contains garbage values causing wrong counts.Step 2: Explain impact of uninitialized array
Incrementing garbage values leads to incorrect frequency counts, so first non-repeating char cannot be found correctly.Final Answer:
count array is not initialized to zero -> Option AQuick Check:
Uninitialized count array causes wrong frequency [OK]
- Forgetting to initialize count array
- Assuming default zero initialization
- Misusing loop conditions
"aabbccddeffg", which character will be identified as the first non-repeating character using a hash map counting method? Consider the code below:
char str[] = "aabbccddeffg";
int count[256] = {0};
for (int i = 0; str[i] != '\0'; i++) {
count[(int)str[i]]++;
}
for (int i = 0; str[i] != '\0'; i++) {
if (count[(int)str[i]] == 1) {
printf("%c", str[i]);
break;
}
}Solution
Step 1: Count frequencies of each character
Characters and counts: a(2), b(2), c(2), d(2), e(1), f(2), g(1)Step 2: Find first character with count 1 in order
Order: a,b,c,d,e,f,g; e and g have count 1, but e appears before g.Step 3: Check carefully the string order
String is "aabbccddeffg"; 'e' appears once at position 8, 'g' at position 11. So first non-repeating is 'e'.Final Answer:
e -> Option BQuick Check:
First unique char in string = e [OK]
- Choosing last unique char instead of first
- Ignoring character order in string
- Miscounting character frequencies
Ready to prove it?
Pass the test and this pattern moves unknown
