0
0
PythonProgramBeginner · 2 min read

Python Program to Find First Non Repeating Character

You can find the first non repeating character in a string using collections.Counter to count characters and then iterate to find the first with count 1, like this: from collections import Counter; s = 'yourstring'; counts = Counter(s); next((ch for ch in s if counts[ch] == 1), None).
📋

Examples

Inputleetcode
Outputl
Inputaabbccdde
Outpute
Inputaabbcc
OutputNone
🧠

How to Think About It

To find the first non repeating character, first count how many times each character appears in the string. Then, look at the string from the start and pick the first character whose count is exactly one. If none is found, return None.
📐

Algorithm

1
Get the input string.
2
Count the occurrences of each character.
3
Go through the string from the beginning.
4
Check if the current character's count is 1.
5
Return the first character with count 1 or None if none found.
💻

Code

python
from collections import Counter

def first_non_repeating_char(s: str) -> str | None:
    counts = Counter(s)
    for ch in s:
        if counts[ch] == 1:
            return ch
    return None

# Example usage
print(first_non_repeating_char('leetcode'))  # Output: l
Output
l
🔍

Dry Run

Let's trace the input 'leetcode' through the code

1

Count characters

counts = {'l':1, 'e':3, 't':1, 'c':1, 'o':1, 'd':1}

2

Iterate over string

Check 'l' count = 1 → return 'l'

CharacterCountIs First Non Repeating?
l1Yes → Return 'l'
e3No
e3No
t1Would check if first not found
c1Would check if first not found
o1Would check if first not found
d1Would check if first not found
e3No
💡

Why This Works

Step 1: Count characters

Using Counter counts how many times each character appears, so we know which repeat.

Step 2: Find first unique

Iterating the string in order lets us find the first character with count 1, which is the first non repeating.

Step 3: Return result

If no character has count 1, return None to show no unique character exists.

🔄

Alternative Approaches

Using dictionary to count manually
python
def first_non_repeating_char_manual(s):
    counts = {}
    for ch in s:
        counts[ch] = counts.get(ch, 0) + 1
    for ch in s:
        if counts[ch] == 1:
            return ch
    return None

print(first_non_repeating_char_manual('leetcode'))
This avoids importing but is longer; performance is similar.
Using index and count methods
python
def first_non_repeating_char_index(s):
    for ch in s:
        if s.count(ch) == 1:
            return ch
    return None

print(first_non_repeating_char_index('leetcode'))
Simple but inefficient for long strings because <code>count</code> scans string each time.

Complexity: O(n) time, O(n) space

Time Complexity

Counting characters takes O(n), and iterating again to find the first unique is O(n), so total is O(n).

Space Complexity

Extra space is used for the count dictionary storing up to n characters, so O(n) space.

Which Approach is Fastest?

Using Counter is fastest and cleanest; using count() in a loop is slowest due to repeated scanning.

ApproachTimeSpaceBest For
collections.CounterO(n)O(n)Efficient and clean for all string sizes
Manual dictionary countO(n)O(n)No imports, similar performance
Using str.count in loopO(n^2)O(1)Very short code but slow for large strings
💡
Use collections.Counter for clean and efficient counting of characters.
⚠️
Beginners often forget to check characters in original order, causing wrong results.