Educatifu
Open menu

First Non-Repeating Character

Mediumstringshashing

Find the first character in a string that appears exactly once.

Input

A single line: a string s of lower-case English letters.

Output

The first character that occurs exactly once, or none if every character repeats.

Examples

Example 1

Input
leetcode
Output
l

l is the first letter that appears only once.

Example 2

Input
aabbcc
Output
none

Every letter repeats, so none.

Example 3

Input
abcabd
Output
c

a and b repeat; c is the first singleton.

Constraints

  • 1 <= length of s <= 10^5
  • s consists of lower-case English letters.

Hints

Reveal hint 1

First count how many times each character appears.

Reveal hint 2

Then scan the string left to right and return the first character whose count is 1.

Reference solution (spoiler — try it yourself first)

Approach

Count character frequencies in one pass, then scan again for the first character with frequency 1.

from collections import Counter
s = input()
c = Counter(s)
print(next((ch for ch in s if c[ch] == 1), "none"))

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation