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 <= 105
s
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:
The brute force approach to finding the first unique character involves checking each character in the string against every other character. We're essentially trying every possible comparison to see if a character appears only once. If it does, we've found our answer.
Here's how the algorithm would work step-by-step:
def find_first_unique_character(input_string):
string_length = len(input_string)
for current_index in range(string_length):
is_unique = True
# Compare the current character to the rest of the string
for comparison_index in range(string_length):
# Skip comparing the character to itself
if current_index == comparison_index:
continue
# If we find a matching character, it is not unique
if input_string[current_index] == input_string[comparison_index]:
is_unique = False
break
# If after comparing to all characters the current character remains unique, we return
if is_unique:
return current_index
# If no unique character found, return -1
return -1
The best way to find the first unique character in a string is to first count how many times each character appears. Then, go back through the string and find the first character that only appeared once.
Here's how the algorithm would work step-by-step:
def find_first_unique_character(input_string):
character_counts = {}
for character in input_string:
character_counts[character] = character_counts.get(character, 0) + 1
# Iterate through the string to find the first unique character.
for character in input_string:
# Check if the character count is equal to 1
if character_counts[character] == 1:
return character
# If no unique character is found, return an empty string.
return ''
Case | How to Handle |
---|---|
Null or empty input string | Return -1 immediately as there are no characters to evaluate. |
String with a single character | Return 0, as the first character is trivially unique. |
String with all identical characters | The solution should correctly identify that no character is unique and return -1 after iterating through the entire string. |
Very long string (approaching maximum string length) | Ensure the solution's time and space complexity are acceptable; hash map or character count array approaches are generally efficient. |
String with only repeating characters at the beginning and a single unique character at the end | The algorithm should correctly identify the unique character at the end, as it will be encountered last. |
String containing non-ASCII characters (Unicode) | Ensure the character counting mechanism (e.g., hash map) can handle the full range of Unicode characters correctly. |
String with multiple unique characters; return the first one | The solution needs to stop at the first encountered unique character and return its index, not continue searching. |
String with mixed case letters (e.g., 'a' and 'A') | The problem did not specify case sensitivity, so either treat them as unique or normalize the string to lower/upper case before processing based on clarification with the interviewer. |