You are given a string s
consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s
until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = "abbaca" Output: "ca" Explanation: For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Example 2:
Input: s = "azxxzy" Output: "ay"
Constraints:
1 <= s.length <= 105
s
consists of 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 tackles this problem by repeatedly scanning the string to find and remove adjacent duplicate characters. It keeps going until no more duplicates can be found. This method checks every possible pair of adjacent characters in each pass.
Here's how the algorithm would work step-by-step:
def remove_duplicates_brute_force(input_string):
string_changed = True
while string_changed:
string_changed = False
index = 0
while index < len(input_string) - 1:
# Check for adjacent duplicates
if input_string[index] == input_string[index + 1]:
# Remove the duplicate characters
input_string = input_string[:index] + input_string[index + 2:]
string_changed = True
# Reset index to start from the beginning
index = 0
else:
# Move to the next character
index += 1
return input_string
The best way to remove adjacent duplicate characters from a string is to use a temporary storage to remember the last seen character. This method efficiently removes duplicates by keeping track of characters.
Here's how the algorithm would work step-by-step:
def remove_adjacent_duplicates(input_string):
temporary_storage = []
for character in input_string:
# Compare current char to the last in temporary_storage.
if temporary_storage and character == temporary_storage[-1]:
temporary_storage.pop()
# Append when temporary_storage is empty or chars differ.
else:
temporary_storage.append(character)
# Construct final string from the storage.
return ''.join(temporary_storage)
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string immediately or throw an IllegalArgumentException based on the requirements. |
String with a single character | Return the string as is, since no adjacent duplicates can exist. |
String with only two characters that are duplicates | The algorithm should remove both characters, resulting in an empty string. |
String with many repeating adjacent duplicates, such as 'aaaaaaa' | The algorithm must efficiently remove all adjacent duplicates until none remain. |
String with interleaved duplicates, such as 'abccba' | The algorithm should correctly remove the 'cc' and result in 'abba', followed by removing 'bb' resulting in 'aa', and then removing 'aa' resulting in ''. |
Very long string to test performance | The stack-based approach or iterative approach should provide efficient performance within the allowed time limit. |
String with no duplicates at all | The algorithm should return the string unchanged. |
String with characters other than lowercase English letters | Either return an error, throw an exception, or filter non-lowercase English characters before processing, based on problem constraints. |