Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Explanation:
The character 'l' at index 0 is the first character that does not occur at any other index.
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
1 <= s.length <= 105s consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are looking for the first person in a line who has a unique hat that no one else is wearing. The brute force approach involves taking each person one by one and checking the entire rest of the line to see if anyone else is wearing the exact same hat.
Here's how the algorithm would work step-by-step:
def first_uniq_char(text_string):
# Iterate through each character to test if it appears anywhere else in the string
for current_index in range(len(text_string)):
current_char = text_string[current_index]
is_duplicate_found = False
for comparison_index in range(len(text_string)):
if current_index != comparison_index and text_string[comparison_index] == current_char:
is_duplicate_found = True
break
# Stop and return the index immediately when we find a character with no duplicates
if not is_duplicate_found:
return current_index
# Return negative one if every character in the string has at least one duplicate
return -1To quickly find the very first letter that does not repeat, we can use a two-pass strategy. Instead of constantly checking and re-checking letters against each other, we first gather a quick count of how many times each letter appears.
Here's how the algorithm would work step-by-step:
def first_uniq_char(input_string):
character_tally = {}
# We build a frequency map first to know the total count of every character in constant time.
for current_character in input_string:
if current_character in character_tally:
character_tally[current_character] += 1
else:
character_tally[current_character] = 1
# A second pass in original order is required because we need the lowest index, not lowest frequency.
for index_position, current_character in enumerate(input_string):
if character_tally[current_character] == 1:
return index_position
# Returning negative one guarantees we signal the absence of any non-repeating characters.
return -1| Case | How to Handle |
|---|---|
| Empty string input | Return -1 immediately since no characters exist to evaluate. |
| String with a single character | Return index 0 because a single character is inherently unique. |
| All characters in the string are repeating | Return -1 as the frequency map will show no count equals one. |
| String with all unique characters | Return index 0 because the very first character is the first unique one. |
| Very large string exceeding standard memory limits | Use a fixed-size frequency array of size 26 for lowercase English letters to maintain O(1) space complexity. |
| String containing uppercase, special, or unicode characters | Use a hash map instead of a fixed array to safely track counts for any character set without out-of-bounds errors. |
| First unique character appears late in a very long string | Use a two-pass approach where the second pass iterates through the original string indices to find the first character with a count of one. |