Facebook Pixel

First Unique Character

Given a string s, find the index of the first character that appears exactly once. If no such character exists, return -1.

Input

  • s: a string

Output

An integer - the index of the first unique character, or -1 if none exists

Examples

Example 1:

Input: s = "leetcode"

Output: 0

Explanation: The character 'l' at index 0 appears only once. It is the first unique character.

Example 2:

Input: s = "loveleetcode"

Output: 2

Explanation: Character frequencies: l(3), o(2), v(1), e(4), t(1), c(1), d(1). The first character that appears only once is 'v' at index 2.

Example 3:

Input: s = "aabb"

Output: -1

Explanation: All characters appear more than once.

Try It Yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro