Doubt in one question

I am solving this question from leetcdoe…
1081. Smallest Subsequence of Distinct Characters

Here we have to print smallest subsequnece which should have all distict character of a given string.
I am not able to come up with any nice approach, here i can find many solution on discussion, but not able to understand them clearly.
Can you help me to come up with nice logic, instead of memorising solution this would be better.
Thanks

Hi Ashish, use a stack!
the main idea is for current character, if previous character is higher(lexographically greater) and have more than 1 occurences, then it is better to take that previous character later as this reduces the string lexographically!
eg:

for bcacb
answer is acb because
when we are at index 2
string is bca but
a<c and c occurs later also, so remove c
string becomes - ba
again, a<b and b occurs later too, so remove b
string becomes a
after that add c and b as they come!
my code - https://ide.codingblocks.com/s/345407

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.