LeetCode 1081. Smallest Subsequence of Distinct Characters
原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
题目:
Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.
Example 1:
Input: "cdadabcc"
Output: "adbc"
Example 2:
Input: "abcd"
Output: "abcd"
Example 3:
Input: "ecbacba"
Output: "eacb"
Example 4:
Input: "leetcode"
Output: "letcod"
Note:
1 <= text.length <= 1000textconsists of lowercase English letters.
题解:
Try to build the string greedily. Mark the last appearence of each char in text.
For current char c, if it is not already added. Keep check the last added char. If it is bigger than current c and that bigger char last appearence is after current index, then it means that this bigger char could be added later.
Pop it out and mark it as unused.
Now this makes sure that the char in stack are as small as possible.
Time Complexity: O(n). n = text.length().
Space: O(1). size 26.
AC Java:
class Solution {
public String smallestSubsequence(String text) {
int [] last = new int[26];
for(int i = 0; i<text.length(); i++){
last[text.charAt(i)-'a'] = i;
}
Stack<Character> stk = new Stack<>();
boolean [] used = new boolean[26];
for(int i = 0; i<text.length(); i++){
char c = text.charAt(i);
if(used[c-'a']){
continue;
}
while(!stk.isEmpty() && stk.peek()>c && last[stk.peek()-'a']>i){
char top = stk.pop();
used[top-'a'] = false;
}
stk.push(c);
used[c-'a'] = true;
}
StringBuilder sb = new StringBuilder();
for(char c : stk){
sb.append(c);
}
return sb.toString();
}
}
LeetCode 1081. Smallest Subsequence of Distinct Characters的更多相关文章
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
随机推荐
- golang基础语法
golang语言的常量定义: const filename="abc.txt"; const filename String="abc.txt" golang ...
- 移动端可视化框架antv f2出现两个legend选项
前天遇到个坑,把我给坑死了 ,在帮朋友做一个微信公众号的项目,使用的vue全家桶,有个模块需要用到数据可视化展现,之前做项目的时候用过antv,比较熟悉,因为是移动端的项目,所以用的是antv f2这 ...
- MIME类型对应表:
MIME类型对应表: 常用MIME类型: 扩展名 MIME类型 .iso ISO File .rar application/x-rar-compressed .zip application/zip ...
- WPF设置全局控件样式
原文:WPF设置全局控件样式 方法: 在资源文件APP.XAML中添加如下资源 <Application x:Class="_360UI.App" xmlns="h ...
- C# Winform控件字体大小自适应
using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WGClie ...
- YAML语言简明教程
编程免不了要写配置文件,如果你还在用xml/ini/json,就有点过时了,怎么写配置也是一门学问. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. 本文介绍 YAM ...
- 91.用js遍历原生json数据
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Java实现简易聊天室
Java实现简易聊天室 在学习<Java从入门到精通>这本书,网络通信,基于TCP实现的简易聊天室,我这里对书中的代码略做了修改,做个记录. 这里先放一下运行效果图,代码放在最后. 运行效 ...
- ApplicationContext的名称解释
如果说BeanFactory是Spring的心脏,那么Application就是完整的身躯.ApplicationContext就是由BeanFactory派生出来的. 1.ApplicationCo ...
- css 带换行的垂直居中
span{ display:flex; justify-content:left; align-items:center; height:100%; width:100%; }