#C8522. First Unique Character in a String
First Unique Character in a String
First Unique Character in a String
You are given a string S
consisting only of lowercase English letters. Your task is to find the index \( i \) of the first character in S
that occurs exactly once. If there is no such character, output \( -1 \).
More formally, let S
be a string of length \( n \). You need to find the smallest index \( i \) (0-indexed) such that the frequency of S[i]
in \( S \) is 1. If no such \( i \) exists, return \( -1 \).
Example:
Input: leetcode Output: 0</p>Explanation: 'l' occurs only once and is the first unique character in "leetcode".
inputFormat
The input consists of a single line containing the string S
, where \( 1 \leq |S| \leq 10^5 \) and each character of S
is a lowercase English letter.
outputFormat
Output a single integer representing the index of the first non-repeating character in S
. If there is no such character, output \( -1 \).
leetcode
0
</p>