Ctrl K

First One-Off Letter

Easy
You are given a string text of lowercase English letters. Find the leftmost letter that occurs in the string exactly once and return its index (0-based). If every letter occurs at least twice, return -1.
Example 1
text="teapot" → 1

t occurs twice, while e at index 1 occurs only once.

Example 2
text="abcabd" → 2

a and b repeat; the first letter without a twin is c at index 2.

Example 3
text="xyxy" → -1

Every letter appears twice, so the answer is -1.

Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNote From Clipped Letters EasyWords Heard Only Once EasyWords From Letter Tiles EasyBalloon Signs From Letters EasyLetters Up and Down Easy
loading Python…
Input
text="teapot"
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 93 tests on the server, hidden ones included.