LeetCode-Remove Duplicate Letters
2016-07-19 13:49
253 查看
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
Return
Given
Return
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Analysis:
We maintain a smallest valid char list and iterate through the whole string. When we move to the next char, we want to put it as ahead as possible in the valid char list so we want to remove all chars that are larger than it straing from the tail, e.g., "acd" and the next char is 'b', we then want to put it after 'a'; however, whether we can remove 'd' and 'c' depends on whether there are still 'b' and 'c' in the left string, if yes, then we can safely remove them.
Also, if we cannot remove 'd', then we cannot remove any chars in front of 'd', because this will actually enlarge the lexicographical order. e.g., "acd" and the following is "bc", if we remove "c", then the result is "adbc" which is larger than "acdb".
Solution:
Example:
Given
"bcabc"
Return
"abc"
Given
"cbacdcbc"
Return
"acdb"
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Analysis:
We maintain a smallest valid char list and iterate through the whole string. When we move to the next char, we want to put it as ahead as possible in the valid char list so we want to remove all chars that are larger than it straing from the tail, e.g., "acd" and the next char is 'b', we then want to put it after 'a'; however, whether we can remove 'd' and 'c' depends on whether there are still 'b' and 'c' in the left string, if yes, then we can safely remove them.
Also, if we cannot remove 'd', then we cannot remove any chars in front of 'd', because this will actually enlarge the lexicographical order. e.g., "acd" and the following is "bc", if we remove "c", then the result is "adbc" which is larger than "acdb".
Solution:
public class Solution { public String removeDuplicateLetters(String s) { if (s.length() == 0) return s; int[] charCount = new int[26]; boolean[] inStack = new boolean[26]; Arrays.fill(charCount, 0); Arrays.fill(inStack, false); char[] charArray = s.toCharArray(); for (char c : charArray) { charCount[c - 'a']++; } Stack<Character> resStack = new Stack<Character>(); for (char c : charArray) { int index = c - 'a'; charCount[index]--; if (inStack[index]) continue; while (!resStack.isEmpty() && resStack.peek() > c && charCount[resStack.peek() - 'a'] > 0) { inStack[resStack.pop() - 'a'] = false; } resStack.push(c); inStack[index] = true; } StringBuilder builder = new StringBuilder(); while (!resStack.isEmpty()) { builder.append(resStack.pop()); } return builder.reverse().toString(); } }
相关文章推荐
- svn: E200030: sqlite[S10]: disk I/O error
- UGUI拖拽与射线检测问题(背包前奏)
- Installing the Zend Debugger
- 数据库优化之mysql系统的优化方式
- MyBatis使用大全(2)------入门初体验
- 禁止页面长按复制
- jquery学习基础使用
- rem的使用
- jquery学习进阶使用
- makefile 基础
- Mac Pro Office Word 2011 个性化设置
- 获取设置唯一的UDID的值
- Understand用户手册和参考指南
- SpringMVC 用http请求的Get和Post请求作为路由的方法的重载方式
- iOS开发之瀑布流
- 自动释放池
- 统计项目的总代码行数 Android studio
- android--xUtils开发框架
- Django新手需要注意的10个要点
- 基于H5的微信支付开发详解