Show problems as
Classic problem #387EasyHash TableStringQueueCounting

First Unique Character in a String

Real-world version: First Solo Stop
Tracked in this browser only
Read First Unique Character in a String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstHashmaps · Strings, animated step by step

A courier's shift is written down as the string route. Each position holds one lowercase letter naming the loading bay that was visited at that point in the shift, and a bay can be visited more than once.

A bay is solo when it shows up exactly once in the whole shift. Return the position of the earliest solo bay in route, counting positions from 0. If every bay in the shift was visited two or more times, return -1.

Examples

Example 1

Input
route = "swiss"
Output
1

Bay s is visited three times, so the earliest bay visited once is w at position 1.

Example 2

Input
route = "kayak"
Output
2

Bays k and a are each visited twice, leaving y at position 2 as the only solo bay.

Example 3

Input
route = "gg"
Output
-1

The single bay in this shift is visited twice, so no solo bay exists.

Constraints

  • 1 <= route.length <= 10^5
  • route contains lowercase English letters only

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def first_solo_stop(route: str) -> int:
Java
public int firstSoloStop(String route)