Strings

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Count frequencies, then scan again to find the first count of one.

Step 1 / 5
Problem shape
Current lesson

First Non-Repeating Character Lab

Strings Beginner

First Non-Repeating Character

Welcome to the First Non-Repeating Character algorithm lab. Count frequencies, then scan again to find the first count of one. Use it when: Use when position and uniqueness both matter. The time complexity is O(n), and the space complexity is O(k). The pseudocode is: count every character for i from 0 to n - 1 if count[s[i]] == 1 return i return -1 As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(k)

Pseudocode

count every character
for i from 0 to n - 1
  if count[s[i]] == 1
    return i
return -1
Open deeper structure lesson
1.0x