All problemsBack
First Unique Character in a String
mediumGiven 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 themain harness and Run to verifyInput
s = "leetcode"Expected Output
0Input
s = "loveleetcode"Expected Output
2Input
s = "aabb"Expected Output
-1Constraints
- 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.