我正在做一个leetcode练习
https://leetcode.com/problems/remove-duplicate-letters/
问题是:
# Given a string which contains only lowercase letters, remove duplicate # letters so that every letter appear once and only once. You must make # sure your result is the smallest in lexicographical order among all possible results. # # Example: # Given "bcabc" # Return "abc" # # Given "cbacdcbc" # Return "acdb"
我不太确定what is the smallest in lexicographical order
和为什么给予"cbacdcbc"然后答案将是"acdb"
谢谢你的答案提前:)
给定s(s 1)的第一个字符小于t(t 1)的第一个字符,或者如果它们是等价的,第二个字符,最小的字典顺序是一个顺序关系,其中字符串s小于t.等等
所以aaabbb
小于aaac
因为虽然前三个字符相等,但第四个字符b
小于第四个字符c
.
因为cbacdcbc
,有几个选项,因为b
并且c
是重复的,您可以决定删除哪些重复项.这导致:
cbacdcbc = adbccbacdcbc= adcbcbacdcbc = badccbacdcbc= badc ...
因为adbc
< adcb
,你不能简单地回答你脑海中浮现的第一个答案.