First Unique Character

Medium~15 min

Given a string s, find the first non-repeating character in it and return its index. If no unique character exists, return -1.

A non-repeating character is one that appears exactly once in the entire string.

Examples

Example 1
Input: s = "leetcode"
Output: 0
Explanation: "l" is the first character that appears only once. It is at index 0.
Example 2
Input: s = "loveleetcode"
Output: 2
Explanation: "v" is the first unique character, at index 2. "l", "o", and "e" all repeat.
Example 3
Input: s = "aabb"
Output: -1
Explanation: Every character repeats, so there is no unique character.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only lowercase English letters
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run