387_First Unique Character in a String
[easy] [string, hash table]
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Note:You may assume the string contain only lowercase letters.
Examples:
Solution 1: brute force
Idea:
Loop over each character from left to right, count the number of its occurrence. If it only occur once, return the index.
Time Complexity:
Space Complexity:
Solution 2: Use dictionary
Idea:
Loop over each character from left to right.
Store the character and its index in a dictionary if it's the first time it occur.
If it occur more than once, store the character in another set.
return the minimum index in the dictionary if exists.
Time Complexity:
Space Complexity:
Last updated