How to get the smallest in lexicographical order?

后端 未结 6 1775
抹茶落季
抹茶落季 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: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.

提交回复
热议问题