First Unique Character

easy · hash map, strings

First Unique Character

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

Function signature

func FirstUniqueChar(s string) int

In Python, implement first_unique_char(s).

Example

"leetcode" -> 0
"loveleetcode" -> 2
"aabb" -> -1

Constraints

  • 0 <= len(s) <= 200_000
  • String contains lowercase English letters

Aim for O(n) time.

Try it before coding

For "abac", both b and c have their final count equal to one. Which index must the caller receive, and what supplies the ordering?

Check your reasoning

Return 1. In the original string b occurs at index 1 and c at 3. The counts establish uniqueness and the original positions establish which comes first.

Run tests to see results
No issues detected
    Join Discord