Back

First Unique Character in a String

medium

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Test Cases

Copy an input into the main harness and Run to verify
Input
s = "leetcode"
Expected Output
0
Input
s = "loveleetcode"
Expected Output
2
Input
s = "aabb"
Expected Output
-1

Constraints

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

Hints

Hint 1 — click to reveal

Count frequencies first, then scan for the first count of 1.

Hint 2 — click to reveal

For streaming variants, a queue of candidate characters works.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.