iq.lab
Python starts when a code cell comes near or you run one
easyHash maps and sets target 15 min

First unique character

In a string s, call a character unique if it appears exactly once in the whole string. Return the index (the position, counting from 0) of the leftmost unique character. If no character is unique, return -1.

Example 1
Inputs = "swiss"Output1

s appears three times. w, at index 1, appears once. i at index 2 is unique too, but w comes first.

Example 2
Inputs = "level"Output2

l and e each appear twice. v, at index 2, appears once.

Example 3
Inputs = "noon"Output-1

n and o each appear twice, so no character is unique.

Constraints
  • 0 ≤ len(s) ≤ 2 · 105

  • s holds only the lowercase letters a to z.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.