First Unique Character in a String
Real-world version: First Solo StopThe statement here is our own: the same inputs, outputs and technique.
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.
def first_solo_stop(route: str) -> int:public int firstSoloStop(String route)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Ransom NoteReal-world version: Spell It With TilesEasy
- Maximum Number of BalloonsReal-world version: Kettle Signs from the Tile TrayEasy
- Find Words That Can Be Formed by CharactersReal-world version: Signs Spellable From A Tile TrayEasy
- Substrings of Size Three with Distinct CharactersReal-world version: Three Clean Pigment WindowsEasy