leetcode5086

问题描述

给定一个字符串s,其中只包含小写字母。求s的一个子序列t。要求t包含s中的全部字符。如果答案有多个,输出字典序最小的那个子序列。

解法描述

首先,为s中每个字符打标签,表示该字符是否为其类型中的最后一个字符

其次,从左往右扫描s,让每个字符进栈。进栈过程中满足如下约束:

  • 当栈顶元素为其类型中的最后一个字符,此元素不可弹出
  • 当栈顶元素不是其类型最后一个字符且比当前字符大,那么栈顶元素应该被弹出,因为后面还有这类字符,所以先把它弹出去。这是最重要的贪心。
  • 当栈中已经包含当前字符时,如果当前字符isLast=true,那么栈中的对应字符不可弹出。
import java.util.Arrays;

class Solution {
public String smallestSubsequence(String text) {
//初始化isLast
boolean[] isLast = new boolean[text.length()];
boolean[] had = new boolean[26];
for (int i = text.length() - 1; i >= 0; i--) {
int c = text.charAt(i) - 'a';
if (!had[c]) {
had[c] = true;
isLast[i] = true;
} else {
isLast[i] = false;
}
}
int[] sta = new int[text.length()];//定义一个栈,栈中存放的是字符的下标
int si = 0;//定义一个栈顶指针
int[] indexOf = new int[26];//每类字符在栈中的位置
Arrays.fill(indexOf, -1);//-1表示字符不在栈中
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (indexOf[c - 'a'] != -1) {//栈中已经包含了此字符,则使用最新字符更新之,这个地方很关键
sta[indexOf[c - 'a']] = i;
continue;
}
while (si > 0) {//当可以弹栈的时候尽量弹栈
int pos = sta[si - 1];//栈顶元素的下表
if (isLast[pos]) break;//如果栈顶元素是该类字符中的最后一个,那么不能弹
char top = text.charAt(pos);//栈顶字符
if (top < c) break;//如果栈顶字符小于当前字符,停止弹栈
//否则,执行弹栈
indexOf[top - 'a'] = -1;
si--;//弹栈
}
sta[si++] = i;//当前元素入栈
indexOf[c - 'a'] = si - 1;
}
//构造答案
StringBuilder ans = new StringBuilder();
for (int i = 0; i < si; i++) ans.append(text.charAt(sta[i]));
return ans.toString();
}
}

leetcode5086:smallest-subsequence-of-distinct-characters的更多相关文章

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

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

  2. LeetCode 1081. Smallest Subsequence of Distinct Characters

    原题链接在这里:https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ 题目: Return the le ...

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

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

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

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

  6. Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  7. ✡ leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java

    Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...

  8. LeetCode Longest Substring with At Most Two Distinct Characters

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

  9. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  10. [Locked] Longest Substring with At Most Two Distinct Characters

    Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...

随机推荐

  1. Vue之挂载点、变量、事件、js对象、文本指令、过滤器、事件指令和属性指令

    1.vue导入-挂载点 <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&qu ...

  2. WebSocket协议-基础篇

    本篇文章主要讲述以下几点: WebSocket协议出现的背景 WebSocket与HTTP WebSocket API WebSocket协议出现的背景 我们在上网过程中经常用到的是HTTP和HTTP ...

  3. Python Warning

    Python Warning 官方文档 概念 warning是内置的异常类,所有用户的warning应该继承于内置warning异常. 警告控制的两个阶段: 通过异常过滤器控制警告是否触发,通过fil ...

  4. CSP考前复习

    前言 因为loceaner太菜了,他什么东西都不会 所以他打算学一个东西就记录一下 不过因为他很菜,所以他不会写原理-- 而且,他希望在2019CSP之前不会断更 就酱紫,就是写给他自己的--因为他太 ...

  5. Codeforces Round 573 (Div.1) 题解

    这场怎么说呢……有喜有悲吧. 开场先秒了 A.看到 B,感觉有点意思,WA 了 2 发后也过了. 此时还在 rk 前 200. 开 C,一看就不可做.跟榜,切 D 人数是 C 的两倍. 开 D.一眼感 ...

  6. Java 函数式编程(Lambda表达式)与Stream API

    1 函数式编程 函数式编程(Functional Programming)是编程范式的一种.最常见的编程范式是命令式编程(Impera Programming),比如面向过程.面向对象编程都属于命令式 ...

  7. [LeetCode] 547. Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  8. 第09组 Beta冲刺(5/5)

    队名:观光队 链接 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 学习 展示GitHub当日代码/文档签入记录 无 接下来的计划 无 还剩下哪些任务 s 答辩 遇到 ...

  9. ECMAScript 初探 - 基础篇

    ECMAScript 语言的标准是由 Netscape.Sun.微软.Borland 等公司基于 JavaScript 和 JScript 锤炼.定义出来的. ECMAScript 仅仅是一个描述,定 ...

  10. mybatis 的 dao 接口跟 xml 文件里面的 sql 是如何建立关系的?

    mybatis 会先解析这些xml 文件,通过 xml 文件里面的命名空间 (namespace)跟dao 建立关系:然后 xml 中的每段 sql 会有一个id 跟 dao 中的接口进行关联. 那么 ...