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 <= 1000
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();
}
}
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 ...
随机推荐
- Python 3 + Selenium 3 简单入门学习示例 126邮箱登录
这是一个很多基础演示的书上的例子,但是一般按照这些书上的代码可能都不能成功登录.也许是网易修改了126的页面导致的吧,下面给出最新的能够work的版本 from selenium import web ...
- Java学习:JDBC各类详解
JDBC各个类详解 代码实现: //1.导入驱动jar包 //2.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //3.获取数据库连对象 ...
- 【华为云实战开发】10.经典的C++项目怎么在云端开发?【华为云技术分享】
1 概述 1.1 文章目的 本文主要想为研发C++项目的企业或个人提供上云指导,通过本文中的示例项目 “音频解析器”,为开发者提供包括项目管理,代码托管,代码检查,编译构建,测试管理的操作指导,覆盖软 ...
- 1、VUE介绍
1.VUE简介 最近一段时间,Web前端领域出现了很多MVVM框架技术,如AngularJS.React,VUE.js等等. Vue.js借鉴了AngularJS的设计理念,也吸取了React和Ang ...
- 修改host文件加速访问github
修改本地电脑系统 hosts 文件C:\Windows\System32\drivers\etc,直接在最后加入以下代码 192.30.253.112 github.com 192.30.253.11 ...
- 用友U9 UFSoft.UBF.Business.Session
Session的概念 在现在UBF中,Session的本意是work unit,即持久层的一个边界,非常轻,主要用作批量提交,并标识这次批量提交的边界,不涉及到事务等概念. 当前ISession可以通 ...
- [Flutter] 实现Flutter App内更新
app内实现根据安卓和IOS平台进行更新 时间匆忙,相关操作以及信息都写在代码注释里面了,闲时在补充和完善. 功能在android项目中测试可用,ios上还未进行测试,如果ios有问题或者没问题的话都 ...
- python学习之:序列类型 之列表,元组,range
列表 列表是可变序列,通常用于存放同类项目的集合(其中精确的相似程度将根据应用而变化). class list([iterable]) 可以用多种方式构建列表: 使用一对方括号来表示空列表: [ ] ...
- Python 的稀疏矩阵
什么是稀疏矩阵 简单的说,如果一个矩阵中大部分元素为0,就被称为稀疏矩阵. 对于稀疏矩阵而言,实际存储的数据项很少.如果在计算机中采用传统的二维数组(Python中用二维列表)来存储稀疏矩阵,就会浪费 ...
- idea中导入Maven项目,但是没有Maven Dependencies,且没有pom
1. 可以复制其他项目的类似的pom.xml文件复制到当前项目下 2. 导入此项目为maven项目 3. 配置相应的maven环境 4. 如果此时的Application.class中的main方法没 ...