How to get the smallest in lexicographical order?

后端 未结 6 1774
抹茶落季
抹茶落季 2021-02-05 07:43

I am doing a leetcode exercise

https://leetcode.com/problems/remove-duplicate-letters/

The question is:

# Given a string which contains only lowe         


        
相关标签:
6条回答
  • 2021-02-05 08:26

    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

    0 讨论(0)
  • 2021-02-05 08:29

    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 = adbc
    cbacdcbc = adcb
    cbacdcbc = badc
    cbacdcbc = badc
    ...
    

    since adbc < adcb, you cannot thus simply answer with the first answer that pops into your mind.

    0 讨论(0)
  • 2021-02-05 08:36

    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
    
    0 讨论(0)
  • 2021-02-05 08:37

    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.

    0 讨论(0)
  • 2021-02-05 08:40

    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.

    0 讨论(0)
  • 2021-02-05 08:41

    String comparison usually can be done in 2 ways:

    • compare for first unmatched letter (called lexicographical ) for example aacccccc is less than ab because at second position b has been met (and a < b).
    • compare string length first and shorter string is treated as less. If strings length are equal then apply lexicographical.

    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

    0 讨论(0)
提交回复
热议问题