Remove Duplicate Letters
Given a string of lowercase letters, remove duplicate letters so each appears exactly once. The result must be the smallest lexicographical result possible while preserving the relative order of first occurrences of each letter.
A monotonic increasing stack with a frequency counter solves this. Count frequencies. Iterate, decrement count for each char. If already used, skip. While the stack top is greater than the current char and the top char appears later, pop the top (mark as unused). Push current char, mark as used. This greedy approach ensures the smallest lexicographical result.
Example Input & Output
No duplicates.
Smallest lexicographical.
Lexicographically smallest.
Algorithm Flow
Solution Approach
Remove duplicate letters from a string to produce the smallest lexicographic result. Use a monotonic stack with a frequency counter and a boolean array tracking used characters. Count character frequencies first. Iterate through each character, decrement its count. If it is already in the result (used), skip it. While the stack is not empty and the current character is smaller than the top of the stack and the top character still appears later, pop the stack and mark it unused. Push the current character.
The stack maintains the result in lexicographic order. A character is popped if a smaller character appears later and the current one is no longer needed. Each character is processed once.
Time complexity is O(n), space complexity is O(k).
Best Answers
import java.util.*;
class Solution {
public String solution(String s) {
int[] cnt = new int[26];
boolean[] used = new boolean[26];
Stack<Character> stack = new Stack<>();
for (char ch : s.toCharArray()) cnt[ch - 'a']++;
for (char ch : s.toCharArray()) {
cnt[ch - 'a']--;
if (used[ch - 'a']) continue;
while (!stack.isEmpty() && ch < stack.peek() && cnt[stack.peek() - 'a'] > 0)
used[stack.pop() - 'a'] = false;
stack.push(ch);
used[ch - 'a'] = true;
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) sb.insert(0, stack.pop());
return sb.toString();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
