Easy foundations

0 attempts recorded
Stored in MariaDB
Recommended next~ 18 min

First Unique Character

EasyHash mapsStringsIterationComplexity

Given a string s, return the index of the first character that appears exactly once. Return -1 if every character repeats.

Example 1Input s = "leetcode"Output 0

The first unique character is "l".

Example 2Input s = "loveleetcode"Output 2
Example 3Input s = "aabb"Output -1
Constraints & assumptions
  • 1 ≤ len(s) ≤ 100,000
  • s contains lowercase English letters
  • Aim for O(n) time
solution.py