You are given a string s
and an integer k
, a k
duplicate removal consists of choosing k
adjacent and equal letters from s
and removing them, causing the left and the right side of the deleted substring to concatenate together.
We repeatedly make k
duplicate removals on s
until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.
Example 1:
Input: s = "abcd", k = 2 Output: "abcd" Explanation: There's nothing to delete.
Example 2:
Input: s = "deeedbbcccbdaa", k = 3 Output: "aa" Explanation: First delete "eee" and "ccc", get "ddbbbdaa" Then delete "bbb", get "dddaa" Finally delete "ddd", get "aa"
Example 3:
Input: s = "pbbcggttciiippooaais", k = 2 Output: "ps"
Constraints:
1 <= s.length <= 105
2 <= k <= 104
s
only contains 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 removing duplicate characters checks every possible substring of adjacent, identical characters. For each substring, we check if it meets the specified removal count, and if so, remove it. This process is repeated until no more removals can be made.
Here's how the algorithm would work step-by-step:
def remove_duplicates_brute_force(input_string, removal_count):
string_length = len(input_string)
while True:
index = 0
removal_made = False
while index < string_length:
adjacent_duplicates_count = 1
next_index = index + 1
# Count adjacent duplicates
while next_index < string_length and input_string[index] == input_string[next_index]:
adjacent_duplicates_count += 1
next_index += 1
# Check if the number of duplicates matches the removal count
if adjacent_duplicates_count == removal_count:
input_string = input_string[:index] + input_string[next_index:]
string_length = len(input_string)
removal_made = True
# Restart from beginning, to check for newly formed adjacent duplicates
index = 0
break
index = next_index
# Stop iterating if no removals were made during a pass.
if not removal_made:
break
return input_string
The goal is to remove consecutive identical characters if they appear 'k' times in a row. The efficient approach uses a memory (like a stack) to keep track of the characters and their counts, allowing us to process the string in a single pass.
Here's how the algorithm would work step-by-step:
def remove_duplicates(input_string, k):
stack = []
for character in input_string:
# Check if stack is non-empty and the top character matches
if stack and stack[-1][0] == character:
stack[-1][1] += 1
if stack[-1][1] == k:
# Remove if the count reaches k
stack.pop()
else:
# Add new character with count 1
stack.append([character, 1])
result = ""
for character, count in stack:
result += character * count
return result
Case | How to Handle |
---|---|
Empty string input | Return an empty string since there are no characters to process. |
Null string input | Return an empty string or throw an IllegalArgumentException, depending on requirements. |
k is 1 | Return an empty string as every character will be removed. |
k is greater than the string length | Return the original string as no removals are possible. |
String with all identical characters and length is a multiple of k | Return an empty string as all characters will be removed. |
String with all identical characters and length is not a multiple of k | Return the remaining characters (string length modulo k) repeated. |
String contains characters with high Unicode values | Ensure character comparison and storage handle extended Unicode characters correctly. |
String with alternating characters repeated less than k times | The stack must not incorrectly remove valid characters when intermediate repeating series are processed. |