Skip to main content
Back to problems
Leetcode
Medium
Hash Maps
Strings
First Unique Character In A String

Find the first character in a string that appears exactly once and return its index.

Acceptance 0%
Problem Statement

Given a string s, identify the first character that occurs exactly once in the string and return its index. If no such character exists, return -1.

A character is considered unique if it appears one time in the entire string. The answer should be the smallest index among all unique characters.

Input Format

  • A single string s.
  • s may contain lowercase letters, uppercase letters, digits, or other visible characters depending on the platform variant.

Output Format

  • Return the index of the first non-repeating character.
  • If every character repeats, return -1.

Constraints

  • The string length is at least 1.
  • A linear-time solution is expected.
  • Use extra space only as needed to track character counts.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "leetcode"

Output

0

Explanation

The character 'l' appears once and is the first unique character, so the answer is index 0.

Example 2

Input

s = "loveleetcode"

Output

2

Explanation

Characters 'l' and 'o' repeat, but 'v' appears exactly once at index 2, which is the first unique character.

Show 1 more example

Example 3

Input

s = "aabb"

Output

-1

Explanation

Every character appears more than once, so there is no unique character.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.