Remove Duplicates from a String
Explore how to remove duplicate characters from a string using two main techniques: one with extra memory using a HashSet for linear time complexity, and another without extra memory but with quadratic time complexity. Understand the trade-offs in space and time complexity for these string manipulation methods.
Statement
Given a string that contains duplicate occurrences of characters, remove the duplicate occurrences such that every character in the string appears only once.
Example
Sample input
abbabcddbabcdeedebc
Expected output
abcde
Try it yourself #
Solution 1
- We keep two pointers or indices, one for the current reading position and one for the current writing position.
- Whenever we encounter the first occurrence of a character, we add it to the HashSet.
- Any character already existing in the HashSet is skipped on any subsequent occurrence.
Below is an overview of the algorithm:
read_pos = 0
write_pos = 0
for each character 'c' in str
if 'c' not found in HashSet
add 'c' to hashset
str[write_pos] = str[read_pos]
write_pos++
read_pos++
Let’s go through this step-by-step: