316. Remove Duplicate Letters
Description
Difficulty: Medium
Related Topics: String, Stack, Greedy, Monotonic Stack
Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
1 | Input: s = "bcabc" |
Example 2:
1 | Input: s = "cbacdcbc" |
Constraints:
- 1 <= s.length <= 104
sconsists of lowercase English letters.
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
Hints/Notes
- With stack and set we can get the substring with no duplicate, but not necessarily the smallest
- Add a counter to count the frequency of letters, then we can pop the letters without worrying there’s no more
Solution
Language: C++
1 | class Solution { |