Problem Details
Day 24 : Lexicographically Smallest Subsequence of Distinct Characters.
Let S = “ababc”. The lexicographically smallest subsequence containing all the distinct characters of strings is “abc”.
Strings consist of only lowercase characters.
You do not need to print anything; it has already been taken care of. Just implement the function.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the length of the string ‘S’.
The next line of the test case contains the string ‘S’.
For each test case return the lexicographically smallest subsequence of ‘S’ that contains all the distinct characters of ‘S’ exactly once.
1 <= T <= 10
1 <= N <= 1000
Time Limit: 1sec
Iterate over all the subsequences of string ‘S’:-
We can find the last occurrence for each character. We can use a stack to keep the characters for the result. We can iterate on the string ‘S’. If the current character is not present in the stack, pop the top of the stack till the element on the top element is greater than the current character and will occur later in the string ‘S’ and mark them as unvisited. We do this because this helps in building lexicographically smaller subsequence. Push the current character in the stack. Mark the current character as visited.
We will implement this as follows:-
Equal Arrays
1-3 Palindrome
Ninja And The Strictly Increasing Array
Maximize
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)
Search Pattern (KMP - Algorithm)