First Unique Character in String

EasyArrays & Hashingfrequency count

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string s, find the index of the first character that appears exactly once in the string. Scanning left to right, return the position of the earliest character whose total count across s is 1. If no such character exists, return -1. The result is a zero-based index into s.

Examples

Input: s = "leetcode"
Output: 0
The character 'l' at index 0 occurs only once, and it is the first such character.
Input: s = "loveleetcode"
Output: 2
Both 'l' and 'o' repeat, but 'v' at index 2 is the first character that appears exactly once.
Input: s = "aabb"
Output: -1
Every character appears more than once, so there is no unique character.

Constraints

  • 1 <= s.length <= 10^5
  • `s` consists only of lowercase English letters.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More frequency count problems