What is an efficient algorithm to removing all duplicates in a string?
For example : aaaabbbccdbdbcd
Required result: abcd
You use a hashtable to store currently discovered keys (access O(1)) and then loop through the array. If a character is in the hashtable, discard it. If it isn't add it to the hashtable and a result string.
Overall: O(n) time (and space).
The naive solution is to search for the character is the result string as you process each one. That O(n2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With