How to get the smallest in lexicographical order?

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

提交回复
热议问题