Skip to main content
All coding problems

First Unique Character in a String

Easyarrays hashingHash map frequency countstringhash-mapfrequency-count

Maintained by Vikas D, software engineer

You are handed a string made up of lowercase English letters. Your job is to locate the earliest position where a character appears only one time across the whole string.

The word 'first' refers to position, not value. A character counts as unique only when its total number of occurrences in the entire string is one. The very first such character, read left to right, is the answer.

Return that character's zero-based index. When no character qualifies, meaning every letter appears at least twice, return -1.

Examples

Input: s = "leetcode"
Output: 0

The letter 'l' at index 0 appears once in the whole string, so it is the first non-repeating character.

Input: s = "loveleetcode"
Output: 2

'l' and 'o' both repeat, but 'v' at index 2 appears only once, making it the earliest unique character.

Input: s = "aabb"
Output: -1

Both 'a' and 'b' appear twice, so there is no character that occurs exactly once and the answer is -1.

Constraints

  • 1 <= s.length <= 10^5
  • s contains only lowercase English letters (a to z).

Visible test cases

  • firstUniqChar("leetcode") == 0
  • firstUniqChar("loveleetcode") == 2
  • firstUniqChar("aabb") == -1
  • firstUniqChar("z") == 0
  • firstUniqChar("aadadaad") == -1
  • firstUniqChar("abcabc") == -1

Hints

Hint 1

You cannot decide whether a character is unique by looking at it once. You need to know its total count across the whole string before judging any single position.

Hint 2

Make one pass to tally how many times each character appears, then a second pass to find the earliest position whose tally is exactly one.

Hint 3

Because the alphabet is fixed at 26 letters, the frequency table has a constant size, so the count storage does not grow with the input length.

Loading the editor...

Approaches

Brute force

Time O(n^2) · Space O(1)

For each position, rescan the rest of the string to check whether that character appears anywhere else.

  1. Loop over every index i in the string.
  2. For each i, loop over every other index j and check whether s[j] equals s[i].
  3. If no other index matches, s[i] is unique, so return i.
  4. If the outer loop finishes without finding a unique character, return -1.

Two-pass frequency count

Time O(n) · Space O(1)

Count each character once, then walk left to right and return the first index whose character has a count of one.

  1. Create a map or fixed array of size 26 to hold per-character counts.
  2. Pass one: iterate the string and increment the count for each character.
  3. Pass two: iterate the string again by index and return the first index where the character's count equals one.
  4. If pass two completes with no match, return -1.

Common mistakes

  • Returning the character itself instead of its index. The problem asks for the position.
  • Trying to decide uniqueness in a single pass, which fails because a later occurrence can disqualify a character you already passed.
  • Returning the index of the rarest character or the last unique one instead of the earliest. Order of first appearance is what matters.
  • Forgetting the all-repeats case and returning 0 or undefined instead of -1.

Follow-up variations

  • What changes if the string can contain Unicode or any printable character rather than just lowercase letters?
  • How would you support a stream where characters arrive one at a time and you must report the current first-unique character after each insertion?
  • How would you return the first character that appears exactly k times instead of exactly once?

Build the rest of your prep