Suppose we have a string s. We have to remove all duplicate characters that have already appeared before. The final string will have same ordering of characters like the actual one.
We can solve this by using ordered dictionary to maintain the insertion order of the characters. The value will be the frequency of those characters, however the frequency values are not important here. After forming the dictionary, we can simply take the keys and join them to get the string.
So, if the input is like s = "bbabcaaccdbaabababc", then the output will be "bacd".
Let us see the following implementation to get better understanding −
from collections import OrderedDict def solve(s): d = OrderedDict() for c in s: if c not in d: d[c] = 0 d[c] += 1 return ''.join(d.keys()) s = "bbabcaaccdbaabababc" print(solve(s))