I am doing a leetcode exercise
https://leetcode.com/problems/remove-duplicate-letters/
The question is:
# Given a string which contains only lowe
the smallest in lexicographical order
- your answer should be a subsequence of initial string, containing one instance of every char.
If there are many such subsequences possible (bca, bac, cab, abc
for the first example), return the smallest one, comparing them as strings (consider string order in vocabulary).
why Given "bcabc" then the answer would be "acdb"
You confused two different examples
The smallest lexicographical order is an order relation where string s is smaller than t, given the first character of s (s1) is smaller than the first character of t (t1), or in case they are equivalent, the second character, etc.
So aaabbb
is smaller than aaac
because although the first three characters are equal, the fourth character b
is smaller than the fourth character c
.
For cbacdcbc
, there are several options, since b
and c
are duplicates, you can decided which duplicates to remove. This results in:
cbacdcbc = adbccbacdcbc= adcbcbacdcbc = badccbacdcbc= badc ...
since adbc
< adcb
, you cannot thus simply answer with the first answer that pops into your mind.
You cannot reorder characters. You can only choose which occurrence to remove in case of duplicated characters.
bcabc
We can remove either first b
or second b
, we can remove either first c
or second c
. All together four outputs:
..abc
.cab.
b.a.c
bca..
Sort these four outputs lexicographically (alphabetically):
abc
bac
bca
cab
And take the first one:
abc
There seems to be some misunderstanding; the example states that for the input bcabc
, the expected output should be abc
, not acdb
, which refers to the input cbacdcbc
.
clearly, the wanted output must contain only letter once. now, from what i understand, you must pick the letters in a manner that will give you the best order when the leftmost letters come before in (abc? ascii?) now you'd ask why "acdb" than and not "abcd". i think you don't take the first "cb" since you more c and b later, but you're have to take the "a" since there's only one coming now. then you must take c 'cause there are no more "d" after the next b. that's why you take c, and then d because no more d's later.
in short, you want to take it with best lexicographical order from low to high, but make sure you take all the letters while iterating over the input string.
String comparison usually can be done in 2 ways:
aacccccc
is less than ab
because at second position b
has been met (and a
< b
).Second one may be faster if length of strings are known.
You question contains small error:
why Given "bcabc" then the answer would be "acdb"
While origin was: "Given "bcabc" Return "abc"". That make sense that abc
should be returned instead of bca