Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

Example:

Given "bcabc"
Return "abc"

Given "cbacdcbc"
Return "acdb"

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

给定一个只含有小写字母的字符串,移除重复的字符,每种字符只出现1次,返回字符串是按字典顺序中最小的组合。

解法:贪心算法Greedy,用1个HashMap或者数组统计出现的字符。然后再遍历一遍数组, 假设position = 0,每次找到字符比position的小就更新position,同时也更新count,当count[i] == 0的时候,这个字符就是我们要找的结果字符串里的第一个字符。之后因为其他字符的count还都 > 1,继续在s.substring(position + 1)的子串里递归查找第二个字符,注意要在这个子串里把第一个字符清除掉。

Java:

public class Solution {
public String removeDuplicateLetters(String s) {
if(s == null || s.length() == 0) {
return s;
} int[] count = new int[26];
char[] res = new char[26];
int len = s.length();
for(int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
} int pos = 0;
for(int i = 0; i < len; i++) {
if(s.charAt(i) < s.charAt(pos)) {
pos = i;
}
count[s.charAt(i) - 'a']--;
if(count[s.charAt(i) - 'a'] == 0) { // found first minimum char
break;
}
} String charToRemove = String.valueOf(s.charAt(pos));
return charToRemove + removeDuplicateLetters(s.substring(pos + 1).replaceAll(charToRemove, ""));
}
}  

Python:

class Solution(object):
def removeDuplicateLetters(self, s):
"""
:type s: str
:rtype: str
"""
remaining = collections.defaultdict(int)
for c in s:
remaining[c] += 1 in_stack, stk = set(), []
for c in s:
if c not in in_stack:
while stk and stk[-1] > c and remaining[stk[-1]]:
in_stack.remove(stk.pop())
stk += c
in_stack.add(c)
remaining[c] -= 1
return "".join(stk)  

C++:

class Solution {
public:
string removeDuplicateLetters(string s) {
int m[256] = {0}, visited[256] = {0};
string res = "0";
for (auto a : s) ++m[a];
for (auto a : s) {
--m[a];
if (visited[a]) continue;
while (a < res.back() && m[res.back()]) {
visited[res.back()] = 0;
res.pop_back();
}
res += a;
visited[a] = 1;
}
return res.substr(1);
}
}; 

All LeetCode Questions List 题目汇总

[LeetCode] 316. Remove Duplicate Letters 移除重复字母的更多相关文章

  1. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  2. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

  3. leetcode 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  4. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  5. 【LeetCode】316. Remove Duplicate Letters 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【leetcode】316. Remove Duplicate Letters

    题目如下: Given a string which contains only lowercase letters, remove duplicate letters so that every l ...

  7. 316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  8. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  9. 316 Remove Duplicate Letters 去除重复字母

    给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...

随机推荐

  1. 动态规划——背包问题python实现(01背包、完全背包、多重背包)

    目录 01背包问题 完全背包问题 多重背包问题 参考: 背包九讲--哔哩哔哩 背包九讲 01背包问题 01背包问题 描述: 有N件物品和一个容量为V的背包. 第i件物品的体积是vi,价值是wi. 求解 ...

  2. Kotlin注解深入解析与实例剖析

    在上一次https://www.cnblogs.com/webor2006/p/11522798.html中学习了Kotlin注解相关的东东,这次继续对Kotlin的注解继续学习: 注解也可以拥有自己 ...

  3. 微信小程序~页面注册page

    一 什么是page() page(),是一个函数,用来注册一个页面, 接受一个object参数, 指定页面的初始数据,生命周期函数,事件处理函数 等等 object参数说明: (1)data (obj ...

  4. 如何使用git,进行项目的管理

    1.首先,现在git上个创建一个项目, 2.然后在本地创建一个springboot工程 3.使用git命令   git init 将这个springboot项目交给git进行管理 4.创建一个dev分 ...

  5. [转]windows10下安装与激活rational rose 2003

    Rational Rose2003安装+激活教程(Win10)(另附安装包+激活软件) 原文链接:https://blog.csdn.net/qq_38388811/article/details/8 ...

  6. Node.js是什么?提供了哪些内容?

    什么是Node.js? Node.js是基于Chrome V8 引擎的 JavaScript运行时(运行环境). Node.js提供了哪些内容? Node.js运行时,JavaScript代码运行时的 ...

  7. circus 进程以及socket 管理工具&&docker运行

    circus 是由mozilla 团队开发基于python 以及zeromq 的进程以及socket 管理的工具,类似supervisord 但是比supervisord 更灵活方便 来自官方的使用比 ...

  8. CSS块元素、行内元素、行内块元素的转换

    一.块元素转行内元素:display:inline 二.行内元素转块元素:display:block div{ display: inline; /*无效 width: 500px; height: ...

  9. 81: luogu3370 hash

    hash 模板题 #include <bits/stdc++.h> using namespace std; #define ULL unsigned long long const UL ...

  10. JS实现Base64编码、解码,即window.atob,window.btoa功能

    window.atob(),window.btoa()方法可以对字符串精选base64编码和解码,但是有些环境比如nuxt的服务端环境没法使用window,所以需要自己实现一个base64的编码解码功 ...