EasyHash TableStringQueueCountingUpdated Sep 2026

387. First Unique Character in a String

Asked at Goldman Sachs

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Hash Table

Companies

1

Problem

You are given a string s. Find the first character that appears exactly once in the whole string, and return its index. If no character is unique, return -1.

"First" means the smallest index. So even if several characters appear only once, you must return the position of the leftmost one. A character is unique only if its total count in the string is exactly 1.

A simple way to solve it: count how many times each letter occurs, then walk the string from left to right and stop at the first letter whose count is 1.

Find the leftmost letter that appears once

sleetcode01234567'l' (green) appears once. 'e' (red) appears three times, so it isnot unique.
In "leetcode", 'l' is the only letter at index 0 that never shows up again, so the answer is 0.

Constraints

  • ●1 <= s.length <= 10^5
  • ●s consists of only lowercase English letters

Examples

Example 1
Inputs = "leetcode"
Output0
WhyThe letter 'l' appears only at index 0, so it is unique and it is the leftmost unique letter. Every other letter in the string appears more than once.
Example 2
Inputs = "loveleetcode"
Output2
Why'l' and 'o' both appear twice, so they are not unique. 'v' appears only at index 2, so that is the first unique character.
Example 3
Inputs = "aabb"
Output-1
Why'a' appears twice and 'b' appears twice, so no character is unique. The answer is -1.

Asked At

1 companies
CompanyDifficulty
Goldman SachsEasyView all Goldman Sachs questions →

How to Think About It

1

Brute force: for each index, scan the whole string to see if that character appears elsewhere. That is O(n²).

2

Key insight: uniqueness depends on the whole string, but "first" depends on order. So do two passes — one to count, one to find the first character whose count is 1.

3

Use a 26-length array for lowercase letters. The second pass walks the string (not the alphabet), which preserves the original order.

4

Walkthrough for "loveleetcode": counts l=2, o=2, v=1, e=4, .... Scanning: l (2), o (2), v (1) -> answer index 2.

5

Streaming follow-up: if characters arrive one by one, keep counts plus a queue of candidates and pop from the front while the front is no longer unique.

Optimal Approach

Step 1: count = [0] * 26; count every character.
Step 2: For i in 0..n-1: if count[s[i]] == 1, return i.
Step 3: Return -1.

Time: O(n). Space: O(1) (26 counters).

Count first, then scan in order

countlettertimesc1d1e3l1o1t1Pass 1: 'e' has count 3 (red), while 'l' has count 1 (green).sleetcode01234567iPass 2: index 0 holds 'l', and count['l'] == 1, so we return 0immediately.count[ch] += 1 for every chthen return first i with count[s[i]] == 1otherwise return -1
Pass 1 counts every letter. Pass 2 walks left to right and returns the first letter whose count is 1.
ComplexityTime O(n)Space O(1)We scan the string twice, and the counter array has a fixed size of 26, so the extra memory does not grow with the input.

What Trips People Up in Real Interviews

1

Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.

2

Returning the character instead of its index.

3

Using s.count(ch) inside a loop — that hides an O(n²) algorithm behind a one-liner.

4

Forgetting to return -1 when every character repeats.

Solution Code

O(n) time
def firstUniqChar(s):
    count = [0] * 26
    for ch in s:
        count[ord(ch) - ord('a')] += 1
    for i, ch in enumerate(s):
        if count[ord(ch) - ord('a')] == 1:
            return i
    return -1

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the First Unique Character in a String problem?

First Unique Character in a String asks for the index of the first character that appears exactly once, or `-1` if every character repeats. It is a two-pass counting problem — the classic "count first, then scan in order" pattern.

How do you solve First Unique Character in a String?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask First Unique Character in a String?

First Unique Character in a String is asked at Goldman Sachs. It is a easy difficulty problem.

What are common mistakes on First Unique Character in a String?
  • Scanning the alphabet in the second pass instead of the string. That finds the alphabetically first unique letter, not the first by position.
  • Returning the character instead of its index.
  • Using `s.count(ch)` inside a loop — that hides an `O(n²)` algorithm behind a one-liner.
  • Forgetting to return `-1` when every character repeats.