dsa · easy

First Unique Character in a String

You are given a string s of lowercase letters. Return the index of the first character that appears exactly once in the whole string. If no such character exists, return -1.

Arguments

Example

s = "loveleetcode"

Counting each letter, l appears twice, o twice, v once — but v is at index 2 and nothing before it appears once, so the answer is 2.

s = "aabb" → every letter repeats → -1.

Constraints

1 <= s.length <= 10^5 s consists of lowercase letters Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
"loveleetcode"

Expected:
2

Example 2

Input:
"aabb"

Expected:
-1

Example 3

Input:
"z"

Expected:
0

Open in the Dojo editor