#C10913. First Non-Repeating Character Index
First Non-Repeating Character Index
First Non-Repeating Character Index
Given a string $s$, your task is to find the index of the first character that does not repeat anywhere in the string. In other words, find the first character that occurs exactly once in $s$.
If no such character exists, output -1
.
Examples:
- For
s = "leetcode"
, the output is0
because 'l' is non-repeating. - For
s = "loveleetcode"
, the output is2
because 'v' is the first non-repeating character. - For
s = "aabb"
, the output is-1
because every character repeats.
The solution should have a time complexity of $O(n)$, where $n$ is the length of the string.
inputFormat
The input consists of a single line containing the string s. The string may include alphabets and is case-sensitive.
outputFormat
Output a single integer, which is the index of the first non-repeating character in the string. If no unique character exists, output -1.## sample
leetcode
0