Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String removeDuplicateLetters(String s) {
- if (s.length() == 0)
- return "";
- int[] count = new int[26];
- int pos = 0; // 紀錄位置
- for (char c : s.toCharArray()) {
- count[c - 'a'] += 1;
- }
- // 找到最小的字串
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) < s.charAt(pos)) {
- pos = i;
- }
- // 0的話表示字母最小是a
- if (--count[s.charAt(i) - 'a'] == 0)
- break;
- }
- //求出每一個最小的位置並把它們加總
- String last = removeDuplicateLetters(s.substring(pos + 1).replaceAll("" + s.charAt(pos), ""));
- return s.charAt(pos) + last;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement