Problem 18
First Non-Repeating Character
Easy · 10 pts · String, Hash Table
Problem
You are given a string of lowercase English letters. Find the first character that appears exactly once in the whole string, and print its zero-based index. If every character appears at least twice, print -1. Count how many times each letter occurs in a first pass. Then scan the string again from left to right and return the first position whose letter has a count of 1.
Input
A single line containing a lowercase string.
Output
Print the zero-based index of the first non-repeating character, or -1.
Constraints
1 <= string length <= 100000; lowercase English letters only
Example input
leetcode
Example output
0
Explanation
In "leetcode", l appears once, e appears three times, and t, c, o and d appear once each. The first character with a count of 1 is 'l' at index 0, so the answer is 0.