原题链接在这里: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. 1 <= text.length <= 1000
  2. text consists 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();
}
}

类似Remove Duplicate Letters.

LeetCode 1081. Smallest Subsequence of Distinct Characters的更多相关文章

  1. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  2. [Swift]LeetCode1081. 不同字符的最小子序列 | Smallest Subsequence of Distinct Characters

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. [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 ...

  4. [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 ...

  5. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  6. 【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 ...

  7. [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 ...

  8. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  9. [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 ...

随机推荐

  1. ZYNQ笔记(7):AXI从口自定义IP封装

    使用 AXI_Lite 从口实现寄存器列表的读写,并且自己封装为一个自定义 IP,以便以后使用.本次记录的是 M_AXI_GP0 接口,此接口是 ARM 作为主机,FPGA 作为从机,配置 FPGA ...

  2. go 学习笔记---chan

    如果说 goroutine 是 Go语言程序的并发体的话,那么 channels 就是它们之间的通信机制.一个 channels 是一个通信机制,它可以让一个 goroutine 通过它给另一个 go ...

  3. 使用Dbvisualizer 连接 Elasticsearch

    Dbvisualizer 安装 从网上下载该软件,并破解激活 下载地址:http://www.ddooo.com/softdown/142713.htm 1.下载解压,得到dbvisualizer p ...

  4. 【WPF】2、美化控件

    控件有默认样式,但是有时候默认样式并不够用,就需要美化. 1.常用的方法是美术出图,直接贴图进去,效果又好又简单(对程序来说). 用图片有三种方式:设置控件背景图片.设置控件内容为图片和直接使用图片做 ...

  5. Winform串口编程---接收数据demo(VSPD虚拟串口)

    参考地址:https://blog.csdn.net/memgxingfeixiang/article/details/52513970  https://blog.csdn.net/kevin_io ...

  6. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile

    更换当前jdk版本为项目所需jdk版本即可

  7. StarGAN学习笔记

    11 December 2019 20:32 来自 <https://zhuanlan.zhihu.com/p/44563641>     StarGAN StarGAN是CVPR2018 ...

  8. SpringMVC+EasyUI实现页面左侧导航菜单

    1. 效果图展示 2. 工程目录结构 注意: webapp下的resources目录放置easyui和js(jQuery文件是另外的)                    3. 代码 index.j ...

  9. drf--频率组件

    目录 频率组件简介 自定义频率类 内置频率类及局部使用 全局使用 源码分析 SimpleRateThrottle源码分析 频率组件简介 主要是为了限制用户访问的次数,比如某一个接口(发送验证码)同一个 ...

  10. Django:RestFramework之-------分页

    9.分页操作 分页,看第n页,每页显示n条数据 分页,在n个位置,向后查看n条数据. 加密分页,上一页和下一页 1.基于PageNumberPagination分页 1.路由: url(r'^(?P& ...