【Leetcode】1081. Smallest Subsequence of Distinct Characters
題目地址:
https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
給定一個字串 s s s,要求返回其包含全部字母(每個字母只包含一個)且字典序最小的子序列。題目保證 s s s只含英文小寫字母。
思路是單調棧。參考https://blog.csdn.net/qq_46105170/article/details/108793778。程式碼如下:
class Solution {
public String smallestSubsequence(String s) {
// 求一下每個字母最後出現的位置
int[] pos = new int[26];
for (int i = 0; i < s.length(); i++) {
pos[s.charAt(i) - 'a'] = i;
}
boolean[] used = new boolean[26];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
// sb裡包含的字母直接跳過
if (used[s.charAt(i) - 'a']) {
continue;
}
// 維護棧的嚴格單調遞增性
while (sb.length() > 0 && sb.charAt(sb.length() - 1) > s.charAt(i) && pos[sb.charAt(sb.length() - 1) - 'a'] > i) {
used[sb.charAt(sb.length() - 1) - 'a'] = false;
sb.setLength(sb.length() - 1);
}
sb.append(s.charAt(i));
used[s.charAt(i) - 'a'] = true;
}
return sb.toString();
}
}
時空複雜度 O ( l s ) O(l_s) O(ls)。
相關文章
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- LeetCode #392: Is SubsequenceLeetCode
- leetcode392. Is SubsequenceLeetCode
- Leetcode Distinct SubsequencesLeetCode
- Leetcode Kth Smallest Element in a BSTLeetCode
- Leetcode: Arithmetic Slices II - SubsequenceLeetCode
- Leetcode-Distinct SubsequencesLeetCode
- Distinct Subsequences leetcode javaLeetCodeJava
- LeetCode之Smallest Range I(Kotlin)LeetCodeKotlin
- LeetCode-Find K Pairs with Smallest SumsLeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- LeetCode-Wiggle SubsequenceLeetCode
- LeetCode-Longest Increasing SubsequenceLeetCode
- LeetCode-Increasing Triplet SubsequenceLeetCode
- LeetCode-Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- 【LeetCode】Increasing Triplet Subsequence(334)LeetCode
- LeetCode 115 Distinct SubsequencesLeetCode
- Leetcode - Smallest Rectangle Enclosing Black PixelsLeetCode
- [LeetCode] 115. Distinct SubsequencesLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 230. Kth Smallest Element in a BSTLeetCode
- LeetCode-Smallest Rectangle Enclosing Black PixelsLeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- LeetCode之1 bit and 2 bit Characters(Kotlin)LeetCodeKotlin
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode 1032. Stream of Characters 4行Trie樹LeetCode
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- LeetCode OJ : 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript