Back to practiceFirst Unique Character in a String
First Unique Character in a String
Given a string, print the index of its first non-repeating character, or -1 if none exists.
Example
Input: leetcode
Output: 0 (l never repeats and appears first)
Approach
Two passes with a hash map: first count every character's frequency, then scan again in order and return the first index whose count is 1.
Editor
s = input()
from collections import Counter
counts = Counter(s)
Run your code to see output here.
Isolated sandbox · not executed on your devicePowered by Judge0 CE (free, self-hosted). Runs in an isolated sandbox — not on your device.
Editor
s = input()
from collections import Counter
counts = Counter(s)
Run your code to see output here.
Isolated sandbox · not executed on your devicePowered by Judge0 CE (free, self-hosted). Runs in an isolated sandbox — not on your device.
Sign in to track solved problems and earn XP.
Community Discussions
No discussions yet
Be the first to start a conversation.