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.
How to Solve
Store occurrences of every character
For every character in string, do following-
decrement number of characters remaining in the string to be analysed
if character is already present in stack, don’t bother
if current character is smaller than last character in stack which occurs later in the string again,
it can be removed and added later e.g. stack = bc remaining string abc then a can pop b and then c
add current character in stack and mark it as visited
pop character from stack and build answer string from back