All problems

First Unique Character in a String

Easy · hash map

Given a string s, return the index of the first letter that appears exactly once in the whole string. Indices start at 0 and the first one means the smallest index.

If every letter appears more than once, return -1.

Examples

Example 1

Input: s = "level"

Output: 2

The letters "l" and "e" both appear twice, while "v" appears once, at index 2.

Example 2

Input: s = "abab"

Output: -1

Both letters appear twice, so no letter is unique.

Example 3

Input: s = "xyzx"

Output: 1

The first "x" is not unique, but "y" at index 1 is.

Constraints