Skip to content

First Unique Character

Easy#hash-map#two-pass~15 min

The mission

Problem

Given a piece of text, find the position of the first character that appears exactly once in the whole text. Positions are counted from zero. Return -1 when every character shows up more than once.

Examples

Input:  text = "reference"
Output: 2
Why:    r and e both repeat later, so f is the first character that stands alone
Input:  text = "aabb"
Output: -1
Why:    every character has a twin
Input:  text = ""
Output: -1
Why:    edge case, there is no character to report

Hints

0 / 3

Target time
O(n)
Target space
O(k)

Stuck on the idea rather than the code? Frequency Counting covers it.

Python 3Ctrl+Enter runs
Custom test case