Given a string s consisting of lowercase English letters, return the first letter to appear twice.
Note:
a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.s will contain at least one letter that appears twice.Example 1:
Input: s = "abccbaacz" Output: "c" Explanation: The letter 'a' appears on the indexes 0, 5 and 6. The letter 'b' appears on the indexes 1 and 4. The letter 'c' appears on the indexes 2, 3 and 7. The letter 'z' appears on the index 8. The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
Example 2:
Input: s = "abcdd" Output: "d" Explanation: The only letter that appears twice is 'd' so we return 'd'.
Constraints:
2 <= s.length <= 100s consists of lowercase English letters.s has at least one repeated letter.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 goal is to find the first letter that repeats in a given set of letters. The brute force strategy is to simply check each letter against all the letters that come after it to see if there's a match.
Here's how the algorithm would work step-by-step:
def first_repeated_character_brute_force(input_string):
string_length = len(input_string)
for first_character_index in range(string_length):
first_character = input_string[first_character_index]
# Start from the next character to check for repetition.
for second_character_index in range(first_character_index + 1, string_length):
second_character = input_string[second_character_index]
# Compare the first character with the characters after it.
if first_character == second_character:
return first_character
# If no repeated character is found, return an empty string.
return ''The key idea is to keep track of each letter we've seen. As soon as we find a letter we've seen before, we know that's the answer. This saves us from checking the entire word over and over.
Here's how the algorithm would work step-by-step:
def find_first_repeated_letter(input_string):
seen_characters = set()
for character in input_string:
# Check if the current character has already been seen.
if character in seen_characters:
return character
# If the character is not in seen_characters, add it.
seen_characters.add(character)
#After adding the char, continue
# If no character is repeated, return None.
return None| Case | How to Handle |
|---|---|
| Null or empty string input | Return an appropriate error value or throw an exception since there's no string to process. |
| String with only one character | Return an error or a special value like null, as a repeated character is impossible. |
| String with all identical characters | The first character is guaranteed to repeat as the second character, so return the first character. |
| Very long string approaching memory limits | Consider using a more memory-efficient data structure like a bit vector if the character set is small, or streaming the input if possible. |
| String contains only ASCII characters | Use an array of size 256 as a character set to optimize checking. |
| String contains Unicode characters outside basic ASCII | Use a hash map to track character counts for a larger character set. |
| String with no repeating characters | Return a specific value like null or an empty character to signify no repeating character found. |
| String with extremely long run of unique characters followed by a single repeating character | The solution may take longer to process due to iterating through the unique run, so consider optimizing for early termination if performance is critical. |