First Non-Repeating Character

Hard ⏱ 18 min 39% acceptance ★★★★★ 4.9
Write first_unique_char_index(s) that returns the index of the first character in s that does not repeat anywhere else in the string. If every character repeats, return -1.

Examples

Example 1
Input
s = 'leetcode'
Output
0
Explanation

'l' never appears again in the string, and it's the earliest such character.

Example 2
Input
s = 'aabb'
Output
-1
Explanation

Every character appears at least twice.

Example 3
Input
s = 'loveleetcode'
Output
2
Explanation

'v' at index 2 is the first character with a total count of 1.

Constraints

  • 0 <= len(s) <= 10^5

Topics

StringsCharacter Frequency

Companies

AmazonSalesforceAdobe

Hints

Hint 1

First pass: build a full frequency count with a dictionary.

Hint 2

Second pass: return the first index whose character has count 1.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.