📘
First Unique Character in a String
Given a string s, find the first non-repeating character and return its index. Return -1 if no such character exists.
Example:
Input:
leetcode
Output:
0
Reasoning:
- First, we count the frequency of each character in the string
s = leetcode. - Then, we iterate over the string to find the first character with a frequency of 1.
- We find that the character
lappears only once and it is the first character in the string, so its index is 0. - The final output is the index of the first non-repeating character, which is 0.
Constraints:
- 1 <= len(s) <= 10^5
- s consists of lowercase English letters
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.