Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Check if two strings are anagramsE02. Find the first non-repeating characterE03. Count pattern occurrences with a sliding windowE

Problem No 2

Find the first non-repeating character

Easy

9 minute session

Summary

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

Problem statement

Given a string s, find and return the first character that appears exactly once in s. The input is case-sensitive: 'A' and 'a' are different. If no such character exists, return an empty string ''. If s is empty, return ''. Aim for O(n) time using a single or two-pass approach.

Task

Return the first non-repeating character from a string (case-sensitive). If none exists, return an empty string.

Examples

Example 1

Input

s = "leetcode"

Output

'l'

Explanation

"l" appears once and is the first character with frequency 1.

Input format

One string argument: find_first_non_repeating(s)

Output format

Return a single-character string representing the first non-repeating character, or '' if none.

Constraints

0 <= len(s) <= 10^5. Use O(n) time and O(k) additional space where k is alphabet size.

Samples

Sample input 0

find_first_non_repeating("loveleetcode")

Sample output 0

'v'

Explanation 0

"v" is the first character that appears only once.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:46 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.