Problem No 2
Find the first non-repeating character
Easy≈ 9 minute session
Lesson guide
What this Python exercise practices
Find the first non-repeating character is a beginner practice lesson that focuses on strings, formatting, traversal. It is designed to be solved in about 9 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- String values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 9 minutes
Practice path
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.