Remove Duplicate Letters
来源:互联网 发布:debian ubuntu 知乎 编辑:程序博客网 时间:2024/06/10 02:05
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"
这是一道很有意思的题。首先要求不能重复,第二要求顺序保持一致,并且尽可能排序。如果单单要求不重复,其实非常简单,用SET就能完成。关键是第二个要求,一下子把复杂程度提升了:必须保证顺序一致,而且要尽可能排序。
为了说明步骤,用“bcabc”作为示例:
对于第一个字符 b, 因为 b 第一次出现,我们保留下来。目前字符串为 "b".
对于第二个字符 c, 因为 c 第一次出现,而且比 b 大,我们保留下来。目前字符串为 "bc".
对于第三个字符 a, 因为 a 第一次出现,所以要保留,这点没有疑问。但是 a 比 b 和 c 都小,这里就要考虑两种情况:
第一种情况: 如果在原字符串里,a 后面还有 b 或者 c 或者两个字符都有,这种情况下我们应该不能直接把a放在bc后面。假设在a的后面还有b,那么我们就应该把b移除,把a放在c的后面。
第二种情况:如果在原字符串里,a 后面没有 b 和 c ,这种情况下我们只能直接把a放在bc后面。
public static String removeDuplicateLetters(String s) {if (s == null || s.length() <= 1) return s;// 使用charCounter array,我们可以知道在某一个字符后面是否还有另一个字符。int[] charCounter = new int[256];for (int i = 0; i < s.length(); i++) {charCounter[s.charAt(i)]++;}Set<Character> hashSet = new HashSet<Character>();Stack<Character> stack = new Stack<Character>();for (int i = 0; i < s.length(); i++) {if (!hashSet.contains(s.charAt(i))) {while (stack.isEmpty() == false && stack.peek() > s.charAt(i) && charCounter[stack.peek()] >= 1) {hashSet.remove(stack.peek());stack.pop();}stack.push(s.charAt(i));hashSet.add(s.charAt(i));}charCounter[s.charAt(i)]--;}return stack.toString();}
- leetcode Remove Duplicate Letters
- Remove Duplicate Letters
- Remove Duplicate Letters
- Remove Duplicate Letters
- Remove Duplicate Letters leetCode316
- leetcode: Remove Duplicate Letters
- Leetcode201: Remove Duplicate Letters
- LeetCode Remove Duplicate Letters
- 《leetCode》:Remove Duplicate Letters
- Leetcode: Remove Duplicate Letters
- leetcode--Remove Duplicate Letters
- [Leetcode]Remove Duplicate Letters
- Remove Duplicate Letters
- Remove Duplicate Letters
- 316. Remove Duplicate Letters
- 316. Remove Duplicate Letters
- 316. Remove Duplicate Letters
- Remove Duplicate Letters
- 博客搬家
- ubuntu中vmware出现:Network configuration is missing. Ensure that /etc/vmware/networking exists.的处理方式
- SSH项目开发里用maven的好处
- runtime 完整总结
- 定时器的应用(二):无缝滚动
- Remove Duplicate Letters
- 开发明细原创、问题和hibernate3与4的区别转载
- C#流使用总结
- 多线程中的join()方法
- 【程序员在法国】两记棒喝,惊醒梦中人
- 树莓派:VNC远程登录Raspbian图形界面(tightvncserver)
- 解决thinkpad维持“正在关机”及“启动PWMTR64V.DLL时出现问题”
- hdoj 2680 最短路(dijkstra)
- iOS知识小集 第一期