First Unique Character in a String
1 approach, code in every language
Problem description
Given a string, scan it from left to right and find the earliest character that appears exactly once across the whole string. Report the zero-based index of that character.
If every character in the string repeats, no such character exists and you should return -1.
Example 1
Input: s = "swiss"
Output: 1
's' occurs three times, so it is skipped. 'w' at index 1 is the first character that shows up only once.
Example 2
Input: s = "aabbcc"
Output: -1
Every character appears twice, so there is no character that occurs exactly once.
Constraints
1 <= s.length <= 10^5s contains only lowercase English letters
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
First Solo Stop is our own problem on the technique behind First Unique Character in a String: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free