【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)。
相關文章
- leetcode392. Is SubsequenceLeetCode
- LeetCode之Smallest Range I(Kotlin)LeetCodeKotlin
- [LeetCode] 910. Smallest Range IILeetCode
- [LeetCode] 230. Kth Smallest Element in a BSTLeetCode
- [LeetCode] 115. Distinct SubsequencesLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 674. Longest Continuous Increasing SubsequenceLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode之1 bit and 2 bit Characters(Kotlin)LeetCodeKotlin
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- [LeetCode] 2825. Make String a Subsequence Using Cyclic IncrementsLeetCodeREM
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- LeetCode 1032. Stream of Characters 4行Trie樹LeetCode
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- LeetCode C++ 376. Wiggle Subsequence【Dynamic Programming】中等LeetCodeC++
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- [LeetCode] K-th Smallest Prime Fraction 第K小的質分數LeetCodeFraction質分數
- [LeetCode] 158. Read N Characters Given Read4 II – Call multipleLeetCode
- Missing Subsequence Sum
- Saprk distinct
- Lintcode 1263. Is Subsequence
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Subarray Distinct Values
- [atcoder 349] [F - Subsequence LCM]
- B. Missing Subsequence Sum
- Smallest Range I 最小差值 I
- RxJava_distinct&distinctUntilChangedRxJava
- uniq(uid) distinct uidUI
- 673. Number of Longest Increasing Subsequence
- 7.14 APPROX_COUNT_DISTINCTAPP
- hiho一下 第230周 Smallest Substring