How to get the smallest in lexicographical order?

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

提交回复
热议问题