To find the lexicographically smallest subsequence containing all distinct characters from a string, use a greedy approach with a stack: iterate through each character, and if it's not already in the result, remove characters from the stack that are lexicographically larger and will appear later in the string, then add the current character while tracking visited characters and their frequencies.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
1081. Smallest Subsequence of Distinct Characters | Hindi | Java Solution
Added:Hi friends, so in this video we will see this particular program. This is a very good program friends. So first understand it with patience and then implement it. So basically we will get a string. To return is a subsequence. There are two conditions in the subsequence.
First of all, all the characters should be distinct. And secondly, it should be lexicographically smallest. In simple words, if all the subsequences are arranged in dictionary order, then the first subsequence is required as an answer. Ok? Now let's discuss this a little more. Now, for example, one of its subsequences could be BCA.
Now see in this, the property of distinct is being fulfilled because all three BCA are different. And all the distinct characters in this string are included in it. Now let's take another sequence.
B A & C from Letts. Here we write B.A.C. So friends, we can also skip any character in the sub sequence.
But we have taken care to include all the distinct characters in it. Let's have one of the CABs.
In this also all three characters are coming and that too only once.
And let's say one of these can also be a b c so many such subsequences are possible. Whatever fulfils both these conditions will be the answer. So everyone is fulfilling the first condition. What if we arrange this lexicographically? ABC will come first.
Correct? So that's why its answer is A B C.
Friends, this is the basic problem statement. Now let us see how we can approach this and how we can solve it.
Now let us discuss the approach with another example. So friends, what will you do with the result that will be computed? We will pick each character from here and keep adding it. So I added C.
Then B went to the next character. Now before adding B, let us see C here. So if we talk lexicographically, if we talk about dictionary order, then B comes before C. So we can remove C.
But before removing C, we will ask the question whether C is coming later also? So he will say yes brother, I am coming later.
C is coming here and also coming forward.
So we can remove C. And we will add B here. Then we will proceed to A.
So A comes before B in dictionary order.
But now before removing B, we will ask whether B is coming again? So here we can see B is coming again. So we can remove B without any problem.
And added A. Proceed as usual C. Now C comes only after A. So there is no problem. Let's add this.
Then proceeded D. So friends, D also comes only after C. So we can add D also. Then came C. Now we have already added C, so we will not add this.
So from here we draw a little conclusion that we will have to keep something called visited in which we will know that friends, the person who has already visited should not be added. Right? So we will skip C here. Now we will come here to B. Now before adding B, we will see that B comes after B. So we would like to remove it. But we'll ask is d coming later? So d is not coming after this. So we will not be able to remove d. So to check all this we will have to keep a frequency.
While implementing the code, we will understand better whether brother d is ahead or not. Does he have an appearance or not? To find this out, we can maintain a frequency. So what do we do here? We will add B. And then came C. Now C has arrived. Already visited. Isn't it? If it is there then we will not add it. So ultimately our answer will be this answer. That is why we can see that the answer is A C D B.
I hope we have got some clarity on how to approach this.
Now let us implement and we will get more clarity in the implementation.
So first of all let's create an array of frequencies which will keep count of how many times a character has appeared. Now friends, here I am writing 126. Actually it is given that only small letters will come from small a to small z. And the S key code of small z is 97 and that of z is 122.
So, in between this, we bring SK code.
So I've taken 126. Pretty safe, right? So you can use this. Otherwise, the more specific recommendation is that you should take an array of size 26 and its indexing should be from zero to 25. But there we will have to do some management, A which is 97 will have to be converted into zero.
So I avoid that and take 126. It's not a big number and won't add much load to the program. But this is your choice.
How do you want to do it? I took 126 like this.
Similarly, I take a boolean array which will tell whether it is visited or not. Has that element already appeared in our results? And I will also buy size 126.
What now? First I fill the array with frequencies.
Meaning, I bring out each character one by one. And which character has appeared how many times, like how many times has A appeared, it has appeared twice, it has appeared thrice, I maintain the number of times it has appeared. I write carry and s to the carry array. So basically, one character at a time will come out and appear in ch. And then we go to the CH index and increase its count. So here we write frequency CH plus plus.
After this loop, we have a count of all the characters, showing how many times each character has appeared.
Now basically we had to insert the character and pop it and so on. So for that I take array list to maintain that thing.
As we discussed, if a big character comes, C comes and we have to insert A.
So we were removing like C.
So array list will help us a little in that.
To basically store the characters, we take an array list. Let's name it ST because we will use it as a stack and here we run the loop carry CS to carry array and first of all we check whether it is visited or not. If he is visited it means he has to be skipped. As we had discussed. Here we write visited of ch double = true if it is visited then first thing we reduce its frequency. That's why we are minus the frequency.
And what are we doing from here? Continue that brother, go and fetch the next character.
This character has no work.
Because this is already in our result.
Now first of all we take the size of the stack i.e. how many characters are there in it. So int size = st dot size. And all the operations that will take place will matter only on the size.
So here we write that size > 0. Now size greater than 0 means that in the result we were talking about, some characters are already there in ST. So let's actually fetch the topmost character so that we can compare whether it is bigger or smaller.
Here we write carry tum = st dot gate size -1. Ok? So when indexing happens, we will have to make size -1 to get the last character. And first of all let us check whether the character is bigger than our CH or not. If the right end is big, now we have to check whether it is coming later or not.
Before removing it, we were checking whether it would come later or not. If it is coming later and is also big then we will remove it because it can be added later.
So here let's check frequency time > 0. So friends, you have to understand this. This condition is indicating that this character is coming later. Ok? That's when we were removing it.
Remember? So here we will do ST remove and size - one. That brother, you should remove this character from the team.
Ok? Now mark the visited ones as false.
Mark the falls as visited.
Because now it is not in the result, it will have to be added to the result later. Ok? So here we marked it false.
If it is not fulfilling the condition.
Meaning either the temp is small or it is not coming in the time future. Then what can we do? Then we basically do n't have to take any special steps. We have to enter whatever character comes.
So we will do that work after the while loop. So what do we do to the while loop now? Let's break it and we come out of the vile loop.
Friends, if one character gets removed, then we will go to the other character and try to remove it. So what will we do here? We will also reduce the size by one so that if one character is removed, C may come first.
Removed it. After that, it may happen that B also has to be removed. Because in B we have to insert the character A. Ok? So that's why we reduced the size here by one.
What do we do now? Whatever character was there, we added it.
Add CH and mark visited as true because this has already come in our answer.
So here we write visited CH = true.
And its frequency is also reduced.
Right? Now friends after this loop we have the result in ST. Now basically we have to convert it into a string and return it because what we have to return is a string. Ok? So here we create a string builder that works fast. Whenever we have to do manipulation then String builder is much better. And here we write for carry CH ST, we will keep removing the carry from ST and will keep appending it in the string builder and will just return it here. We convert SP to string and that's it. I think we have it sorted. Let's try running it.
Excellent. Sample test cases are being accepted, friends. Let's submit.
Excellent. Our session is being accepted.
friends. I would request you to like the video. And thank you so much for watching this video. See you in the next video. Yes.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

Gremlin Arrives… While Dorothy May Takes Another Step Forward
The-moons
10K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23

FURIOUS Raskin CORNERS DOJ over Trump DARK PAST!!!!
MeidasTouch
237K views•2026-07-23