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. steam游戏存档迁移

    之前玩的盗版guacamelee等着打折入正,今天入了,不想重新打了,就把存档从盗版迁移了一下. 盗版的目录是F:\Guacamelee\Profile\ALI213\Saves,该目录下又一个SAV ...

  2. 基于springboot2.x集成缓存注解及设置过期时间

    添加以下配置信息: /** * 基于注解添加缓存 */ @Configuration @EnableCaching public class CacheConfig extends CachingCo ...

  3. js插件---WebUploader 如何接收服务端返回的数据

    js插件---WebUploader 如何接收服务端返回的数据 一.总结 一句话总结: uploadSuccess有两个参数,一个是file(上传的文件信息),一个是response(服务器返回的信息 ...

  4. LGOJP3959 宝藏

    题目链接 题目链接 题解 一开始想了一个错误的状压dp,水了40分. 这里先记录一下错误的做法: 错解: 设\(g[i,j,S]\)从\(i\)到\(j\),只经过集合\(S\)中的点的最短路,这个可 ...

  5. 项目Alpha冲刺(团队)-第八天冲刺

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第八天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...

  6. luoguP1120小木棍(POJ - 1011 )

     题意: 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50,个数不超过65. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段 ...

  7. Go语言 - 函数 | 作用域 | 匿名函数 | 闭包 | 内置函数

    函数是组织好的.可重复使用的.用于执行指定任务的代码块.本文介绍了Go语言中函数的相关内容. 介绍 Go语言中支持函数.匿名函数和闭包,并且函数在Go语言中属于“一等公民”. 函数可以赋值给变量 函数 ...

  8. OKR如何解决策略执行问题

    卡普兰和诺顿在2005年发现,十分之九的组织未能执行其战略. 唐纳德·萨尔(Donald Sull)在2015年的研究中发现,有45%的中层管理人员甚至无法说出其组织的重中之重.这些中层经理基本上负责 ...

  9. scylladb docker-compose 用户密码认证配置

    scylladb 对于用户的认证配置还是比较简单的,以下是一个docker-compose 配置的说明 环境准备 docker-compose 文件 version: "3" se ...

  10. 使用rbenv 进行ruby 多版本的管理

    今天需要安装一个ruby 包,但是ruby 版本过低,以前使用过rvm,但是在安装rvm 的时候发现系统有异常 错误信息,发现还有另外一个不错的可选工具 rbenv 安装 我是mac 系统 使用bre ...