代码1
解法:贪心算法Greedy,用1个HashMap或者数组统计出现的字符。然后再遍历一遍数组, 假设position = 0,每次找到字符比position的小就更新position,同时也更新count,当count[i] == 0的时候,这个字符就是我们要找的结果字符串里的第一个字符。之后因为其他字符的count还都 > 1,继续在s.substring(position + 1)的子串里递归查找第二个字符,注意要在这个子串里把第一个字符清除掉。
Runtime: 40 ms, faster than 5.05% of Java online submissions for Remove Duplicate Letters.
class Solution {
public String removeDuplicateLetters(String s) {
int[] arr = new int[26];
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 'a']++;
}
int pos = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(pos) > s.charAt(i)) pos = i;
if (--arr[s.charAt(i) - 'a'] == 0) break;
}
return s.length() == 0? "" : s.charAt(pos) +
removeDuplicateLetters(
s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")
);
}
}