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 1:

Input: "bcabc"
Output: "abc"

Example 2:

Input: "cbacdcbc"
Output: "acdb"

Approach #1: C++. [brute froce]

class Solution {
public:
string removeDuplicateLetters(string s) {
int size = s.size();
if (size == 0) return ""; int l = 0;
string ret = ""; vector<int> cnt(26, 0);
for (int j = 0; j < size; ++j) {
cnt[s[j] - 'a'] = 1;
}
int total_diff = std::accumulate(cnt.begin(), cnt.end(), 0); for (int z = 0; z < total_diff; ++z) {
for (int i = 0; i < 26; ++i) {
int appear = -1;
for (int j = l; j < size; ++j) {
if (s[j] - 'a' == i && ret.find('a' + i) == -1) {
appear = j;
break;
}
}
if (appear == -1) continue; vector<int> cnt2(26, 0);
for (int j = appear; j < size; ++j)
cnt2[s[j] - 'a'] = 1;
for (auto c : ret)
cnt2[c - 'a'] = 1;
int num = std::accumulate(cnt2.begin(), cnt2.end(), 0);
if (num == total_diff) {
ret += char('a' + i);
l = appear + 1;
break;
}
}
} return ret;
}
};

  

Approach #2: C++.

class Solution {
public:
string removeDuplicateLetters(string s) {
vector<int> cand(256, 0);
vector<bool> visited(256, false); for (auto c : s)
cand[c]++; string ret = "0"; for (auto c : s) {
cand[c]--;
if (visited[c]) continue;
while (c < ret.back() && cand[ret.back()]) {
visited[ret.back()] = false;
ret.pop_back();
}
visited[c] = true;
ret += c;
} return ret.substr(1);
}
};

  

reference:

https://leetcode.com/problems/remove-duplicate-letters/discuss/76767/C%2B%2B-simple-solution-easy-understanding

http://www.cplusplus.com/reference/numeric/accumulate/

316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)的更多相关文章

  1. 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ...

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

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

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

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

  4. 316. Remove Duplicate Letters

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

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

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

  6. leetcode 316. Remove Duplicate Letters

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

  7. 【leetcode】316. Remove Duplicate Letters

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

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

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

  9. Remove Duplicate Letters

    316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...

随机推荐

  1. TIMEQUEST学习之黑金动力(三)

    不知不觉,学到的第四章.但是对于TQ的内部模型和外部模型的完整分析还是没有很好的理解.接着学习......... 我们也了解静态时序分析的第一步骤,亦即时钟方面的约束.此外,也稍微对 Report T ...

  2. Py修行路 python基础(一)初识

    编译:把铭文代码执行前,先转成二进制,再执行,这个过程就叫编译. 编译型c,c++go特点:运行效率高依赖编译平台,linux 操作系统 跟CPU交互的接口,与windows不是完全一样不能跨平台,开 ...

  3. 数据结构(c语言描述)

    预备的数学知识 数据结构的概念 基本名词 算法 线性表 线性表的定义和基本操作 顺序表 顺序表增 顺序表删 顺序表查 单链表 建立单链表 单链表增 单链表删 单链表查 双链表 循环链表 静态链表 栈 ...

  4. 04.CSS的继承性和层叠性

    CSS有两大特性:  继承性和层叠性 继承性 面向对象语言都会存在继承的概念 , 在面向对象语言中, 继承的特点:  继承了父类的属性和方法.  那么 css  就是在设置属性的 ,  不会牵扯到方法 ...

  5. Python中正则表达式对中文的匹配问题

    python匹配中文的时候特别要注意的是匹配的正则字符串是否是Unicode格式的: import re source = "s2f程序员杂志一2d3程序员杂志二2d3程序员杂志三2d3程序 ...

  6. Linux VmWare安装虚拟机(centos6.9)

    开启虚拟机 ---------------------------------------------------------------------------------------------- ...

  7. 粗粒度(Coarse-grained)vs细粒度(fine-grained)

    在读的一篇文献中关于RDF的描述: As we know, RDF data is a set of triples with the form (subject, property, object) ...

  8. 2014蓝桥杯B组初赛试题《奇怪的分式》

    题目描述: 上小学的时候,小明经常自己发明新算法.一次,老师出的题目是:     1/4 乘以 8/5      小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)   ...

  9. vim 添加插件

    vim 的功能可以通过向它添加plugin得以扩展.所谓的plugin不过是一个vim会自动载入执行的脚本.把一个脚本放到你的plugin目录就可以了,非常容易. plugin基本上分为两类:  全局 ...

  10. Java虚拟机学习总结之OutOfMemoryError异常

    参考:深入理解java虚拟机一书 开始之前,我们也应当搞清楚连个概念,内存泄漏Memory Leak 内存溢出: 内存泄漏:程序中间动态分配了内存,但是在程序结束时没有释放内存,造成这部分内存不可用. ...