311. First Unique Character in a String

Easy
Frequency Map
Hashing

Problem

Given a string s, find the first character that appears exactly once and return its index. Return -1 if no unique character exists.

Examples

Example 1

Input: "leetcode"
Output: 0

Example 2

Input: "loveleetcode"
Output: 2

Example 3

Input: "aabb"
Output: -1
Constraints

1 <= s.length <= 100000

s consists of lowercase English letters.

Hints

?? Count every character first.

?? Scan the original string again and return the first position whose frequency is one.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

How would you solve this for a stream of characters where the first unique character must be queried repeatedly?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"leetcode"
Expected
0