First Unique Character
EasyPractice library · +10 XPqueuestringfrequency map
Given a string S consisting of lowercase English letters, find the index of the first character that appears only once in the string. If no such character exists, return -1.
Input
A single line containing the string S.
Output
An integer representing the 0-based index of the first unique character, or -1 if none exists.
Constraints
1 <= length of S <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
leetcode
Output
0
The first unique character is 'l' at index 0.
Example 2
Input
loveleetcode
Output
2
The first unique character is 'v' at index 2.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
leetcode
Example 2 input
loveleetcode