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 ...
 
随机推荐
- Lua table concat
			
[1]table concat 简介 使用方式: table.concat(table, sep, start, end) 作用简介: concat是concatenate(连锁.连接)的缩写. ta ...
 - 2019 C/C++《阿里》面试题总结
			
一.C和C++的区别是什么? C是面向过程的语言,C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛. C中函数不能进行重载,C++函数可以重载 C++在C的基础上增添类,C是一个结构化语言 ...
 - Google Chrome 浏览器JS无法更新解决办法
			
JS无法更新原因: 浏览器为了加载快,默认是按照自定规则更新缓存,非实时更新. 我们在开发的时候,JS变动很快,需要即时让浏览器加载最新文件,也就是禁用浏览器缓存 (1)使用F12进入开发者模式,找到 ...
 - JVM故障分析系列之四:jstack生成的Thread Dump日志线程状态
			
JVM故障分析系列之四:jstack生成的Thread Dump日志线程状态 2017年10月25日 Jet Ma JavaPlatform JVM故障分析系列系列文章 JVM故障分析系列之一: ...
 - 无法打开内核设备:\\Global\\vmx86
			
关于如题目的问题,网上有好几个解决办法,这里只介绍最方便的一个办法,利用脚本来解决 @Echo Off title Hankcs's program color 8F CD %~d0 CD %~dp0 ...
 - MongoDB常用数据库命令第一集
			
1.查询操作(1)Help查看命令提示helpdb.help()db.test.help()db.test.find().help()(2)创建/切换数据库use music(3)查询数据库show ...
 - SpringMVC中@RequestParam注解作用
			
1.不使用@RequestParam 请求参数名必须和形参名称一样 2.使用@RequestParam 请求参数名必须和@RequestParam value属性值一样 请求参数名不必和 ...
 - HTML5中重新定义的 b 和 i 元素
			
HTML5强调元素的语义,而非表现.b和i元素是早期HTML遗留下来的产物,它们分别用于将文本变为粗体和斜体(那时CSS还未出现). 当时的规范建议编码人员用strong替代b,用em替代i.不过,事 ...
 - 从一道题看js的拆箱操作
			
前段时间看到一道题,如下:([][[]]+[])[+![]]+([]+{})[!+[]+![]]问最终打印结果,然后简单了解一下js的装箱,拆箱操作. 基本 装箱操作: 就是将基本类型(String, ...
 - 2. vue基础-vue-cli(vue脚手架)
			
1. 作用  快速创建一个基于webpack模板的项目 2. 安装工具 安装webpack:使用npm全局安装webpack,打开命令行工具,输入 npm install webpack -g,安装 ...