原题链接在这里: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. Nginx Tutorial #1: Basic Concepts(转)

    add by zhj: 文章写的很好,适合初学者 原文:https://www.netguru.com/codestories/nginx-tutorial-basics-concepts Intro ...

  2. [CF254C]Anagram(2019-11-15考试)

    题目大意 给你两个长度相同的字符串\(A,B\),要求改变字符串\(A\)中最少的字符,使得字符串\(A\)在排序后和字符串\(B\)相同.输出改变后的字符串\(A\),若多解,输出字典序最小的.\( ...

  3. Java链表操作代码

    /** * */ package com.cherish.SwordRefersToOffer; /** * @author acer * */ public class test_22链表中倒数第k ...

  4. axios捕获401 赋值token

    //捕获401 // http request 拦截器 axios.interceptors.request.use( config => { const token = localStorag ...

  5. .net Dapper 实践系列(6) ---事务删除(Layui+Ajax+Dapper+MySQL)

    目录 写在前面 实现步骤 写在最后 写在前面 上一小节,总结了事务多表更新的两种方法.一个是只更新原来的数据,一个是先删除原来的数据再往里面添加新的数据.而这一小节,说的是事务的多表删除. 实现步骤 ...

  6. ansible超详细使用指南

    在工作中有用到ansible用于自动部署和环境配置,这里整理了一份很详尽的使用指南,如果有用到的可以看看.关于使用ansible自动部署一个网站和docker化,将在下一篇文章中介绍,敬请期待.文章内 ...

  7. 换个语言学一下 Golang (13)——Web表单处理

    介绍 表单是我们平常编写Web应用常用的工具,通过表单我们可以方便的让客户端和服务器进 行数据的交互.对于以前开发过Web的用户来说表单都非常熟悉.表单是一个包含表单元素的区域.表单元素是允许用户在表 ...

  8. vue组件5 组件和v-for指令

    使用v-for遍历一个数组的时候,并且给定的数组变化时vue不会重复生成所有的元素,而是智能的找到需要更改的元素,并只改变这些元素 key属性可以告诉vue数组中的每个元素都应该与页面上的哪个元素相关 ...

  9. js学习之堆栈内存

    **栈内存** >基本数据类型值是直接存放在栈内存中的 栈内存中的变量一般都是已知大小或者有范围上限的,算作一种简单存储.而堆内存存储的对象类型数据对于大小这方面,一般都是未知的.个人认为,这也 ...

  10. 汽车行业如何个性化定制转型?看APS系统在这家企业的运用

    传统汽车行业中往往采用的是按库存推动式生产,一旦市场产生变动就会造成大量的生产,给企业带来大批的资金压力,而另一方面采用按单生产的方式企业往往面临供应链,产能的诸多约束条件限制,稍有不慎就会带来产线停 ...