Skip to content

First Unique Character in a String

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a string s, return the index of the first character that appears exactly once in it. If every character repeats, return -1.

s = "leetcode"       →  0   (l)
s = "loveleetcode"   →  2   (v)
s = "aabb"           →  -1

Constraints

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