How to get the smallest in lexicographical order?

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

提交回复
热议问题