Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the first unique character

Return the first non-repeating character in a string (or an empty string if none).

Python practice8 minHashing & SetsBeginnerLast updated March 26, 2026

Problem statement

Given a string s, find and return the first non-repeating character in it. If every character repeats, return an empty string ''. Your implementation should run in O(n) time where n is the length of the string and use O(1) extra space for character frequencies (assuming fixed alphabet).

Task

Use a hash map (dictionary) to count character frequencies and then find the first character with count 1.

Examples

Basic example

Input

s = "leetcode"

Output

l

The characters 'l', 'e', 't', 'c', 'o', 'd' appear; 'l' is the first that appears only once.

Input format

A single string argument: first_unique_char(s)

Output format

A single-character string (the first unique character) or '' if there is no unique character.

Constraints

0 <= len(s) <= 10^5; s contains printable ASCII characters.

Samples

Sample 1

Input

first_unique_char("loveleetcode")

Output

v

Characters: l->2, o->2, v->1, e->4 so 'v' is the first unique character.