Skip to content
UniCircle
EasyCodingHashing

First character that appears only once

Question DSA-HSH-002

Given a string of lowercase letters, print the index of the first character that occurs exactly once in the whole string. If every character repeats, print -1.

Constraints

  • 1 ≤ length ≤ 100000
  • Only the letters a–z

Input and output

Input
One line: the string s
Output
One integer: the 0-based index, or -1.

Examples

Input 1

banana

Output

0

b appears once.

Input 2

aabbcc

Output

-1

Everything repeats.

Show hints
  • Count first, then search.
  • A 26-slot array is enough for the counts.
Show the explanation

Two passes. First count how many times each letter occurs (26 counters). Then walk the string again and return the first index whose letter has count 1. This is O(n) time and O(1) extra space (26 counters, independent of n).

Original UniCircle question. Last reviewed 21 Oct 2026.