Skip to content
CodeShare
All problems

First Unique Character

Find the first character that appears only once.

EasyStringsHash map

Given a string s, return the index of the first character that appears exactly once in it.

If every character repeats, return -1.

Examples

Example 1
Input
s = "swiss"
Output
1
Why
'w' is the first character that appears only once.
Example 2
Input
s = "aabb"
Output
-1
Example 3
Input
s = "codeshare"
Output
0

Constraints

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