316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appears
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"
- Using stack
- easier than Hard
public String removeDuplicateLetters(String s) {
int[] cnt = new int[26];
for(char c : s.toCharArray()) cnt[c-'a']++;
HashSet<Character> set = new HashSet<>();
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()) {
if(!set.contains(c)) {
while(!stack.isEmpty() && c < stack.peek() && cnt[stack.peek()-'a'] > 0) {
set.remove(stack.pop());
}
stack.push(c);
set.add(c);
}
cnt[c-'a']--;
}
StringBuilder sb = new StringBuilder();
for(char c : stack) sb.append(c);
return sb.toString();
}