Problems › Hashing

First Unique Character

Difficulty: Easy · Topic: Hashing

Given a string s, return the index of the first character that does not repeat anywhere in the string. If there is none, return -1.

Examples

first_uniq_char("leetcode")
→ 0

'l' never repeats.

first_uniq_char("loveleetcode")
→ 2

'v' at index 2 is the first unique.

first_uniq_char("aabb")
→ -1

Target complexity: time O(n), space O(1)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Hashing problems