Problem
Given a string, Write a program to remove duplicate characters from the string.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
There are multiple approach to solve it.
Method 1 : Brute Force
Code
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- 🧺 Space complexity:
O(1)
Method 2 : Using Sorting
These are the steps we can follow:
- Convert string to char array (if not already)
- Sort the elements.
k5kc
becomes5ckk
- Now in a loop, remove duplicates by comparing the current character with previous character.
5ck
Code
|
|
Complexity
- ⏰ Time complexity:
O(nlogn)
- 🧺 Space complexity:
O(1)
Characters are not in order.
Method 3 - Using Hashtable
- Convert the string to character array.
- Store all the characters in the Set.
- Set will store only one instance of each character
- Iterate through Set and print the characters.
- This approach will change the order of characters.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)
Method 4 - LinkedHashset
Here are the algorithm:
- Convert the string to character array.
- Store all the characters in the Linked Hash Set.
- Set will store only one instance of each character
- Iterate through Set and print the characters.
- This approach will retain the order of characters.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
- 🧺 Space complexity:
O(1)
Method 5 - Using array as Hashtable
Similar to approach with hashtable, but if chars are between a-z, or ascii, just use array Here is the procedure
- Convert the string to character array.
- Create the boolean array of 256,( for all 0 – 255 ASCII characters)
- Iterate through character array from step one and set the Boolean array index ( index = ascii value of the character). Ignore if that index is already true.
- Create a separate array based on the index set as true in Boolean array.
- This approach will retain the order of characters.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)
Method 6 - Special Case of Chars Being a…z
Putting the constraint of space and ordering Suppose the problem becomes:
Write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.
Then what solution do we have? Method 2 cannot be used as we want to keep the ordering same. Method 3 cannot be used, as it uses hashtable, which uses extra spaces. Well, nothing is stopping us from using method 1, but it is brute force, and can we do better?
The answer is yes, if the interviewer confirms, that your string is a constrained by say it can have only small case integers. Since one or two additional variables are fine but no buffer is allowed, you can simulate the behaviour of a hashmap by using an integer to store bits instead. This does not work for normal cases. It will only work from a..z case sensitive. No spaces supported. This would work miracles in a 256bit system to process the entire ASCII range. But it does run in O(N). I still +1 this one.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)