316. Remove Duplicate Letters
Given a string s
, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example:
Input: s = "bcabc"
Output: "abc"
Input: s = "cbacdcbc"
Output: "acdb"
Q: Can the input string contain uppercase letters? A: The problem statement does not explicitly mention restrictions regarding uppercase letters, so we assume the string contains lowercase English letters only (a-z).
Q: How do we define lexicographical order? A: Lexicographical order is similar to dictionary order where “a” < “b” < “c” and so forth.
Q: Can the input string be empty? A: Yes, the input can be empty. The output for an empty string should also be an empty string.
To solve this problem efficiently and correctly, we will use a greedy algorithm with a stack:
Here’s the code implementing the above strategy:
def removeDuplicateLetters(s: str) -> str:
# Step 1: Count occurrences of each character in the string
count = {char: 0 for char in s}
for char in s:
count[char] += 1
# Step 2: Use a stack to build the result string
stack = []
in_stack = set()
for char in s:
# Decrease the count of the current character
count[char] -= 1
# If the character is already in the stack, skip it
if char in in_stack:
continue
# Pop from stack if necessary
while stack and char < stack[-1] and count[stack[-1]] > 0:
popped_char = stack.pop()
in_stack.remove(popped_char)
# Add current character to the stack and mark it as in stack
stack.append(char)
in_stack.add(char)
# Join the stack to form the result string
return ''.join(stack)
# Example usage:
print(removeDuplicateLetters("bcabc")) # Output: "abc"
print(removeDuplicateLetters("cbacdcbc")) # Output: "acdb"
The time complexity of this algorithm is (O(n)), where (n) is the length of the string s
. This is because each character is pushed and popped from the stack at most once, ensuring linear time complexity.
The space complexity is (O(k)), where (k) is the number of distinct characters in the string s
. This accounts for the space used by the stack, set, and the dictionary for storing counts, all of which store up to (k) items. In the worst case, (k) can be (26) (number of lowercase English letters), hence it is considered a constant with respect to the input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?