First Unique Character in a String

EasyStringHash Table

Description

Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Examples

Input:s = "leetcode"
Output:0
Explanation:

Counting character frequencies: l=1, e=2, t=1, c=1, o=1, d=1. Scanning left to right, 'l' at index 0 is the first character with count 1.

Input:s = "loveleetcode"
Output:2
Explanation:

Character 'l' appears twice, 'o' appears twice, but 'v' at index 2 appears only once. Scanning from left, 'v' is the first with frequency 1.

Input:s = "aabb"
Output:-1
Explanation:

Both 'a' and 'b' appear exactly twice in the string. Since every character repeats, there is no unique character to return.

Constraints

  • 1 ≤ s.length ≤ 10⁵
  • s consists of only lowercase English letters.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!