AlgoFrames

First unique character

Standard · Write code · about 10 min

Given a string s, return the index of the first character that appears exactly once in s. Return -1 if no character is unique.

Example 1

Input:

["leetcode"]

Output:

0

l appears once, and it comes first.

Example 2

Input:

["loveleetcode"]

Output:

2

l, o repeat later. v is the first that never repeats.

Constraints

  • 0 ≤ s.length ≤ 10,000
  • s uses lowercase English letters

Learn the idea first: Hash sets and maps in practice: counting, lookup, deduplication, a visual lesson.

More Hash sets and maps in practice: counting, lookup, deduplication problems