Skip to content
BytePatterns

First Unique Character

EasyStrings#hash-map#two-pass~15m

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

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