Approach Summary
Monotonic-increasing stack. For each char: pop while stack top > current AND the top appears again later. Skip chars already in the stack.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Solution Progression (Brute Force → Most Optimized)
Understand the architectural evolution from naive baseline to optimal complexity.
| Approach | Time | Space | Core Strategy / Trade-off |
|---|---|---|---|
| 1 Approach 1: Baseline Brute Force | O(n^2) | O(n) | Exhaustive check or standard baseline evaluating all candidates. |
| 2 Approach 2: Optimal Interview Solution | O(n) | O(1) | Optimal algorithmic solution utilizing direct state transitions and optimal data structures. |
Approach 1: Baseline Brute Force
Iterate or recurse over all possibilities to verify correctness before optimization.
Approach 1: Baseline Brute Force Implementation
Approach 1: Baseline Brute Force
class Solution {
public String removeDuplicateLetters(String s) {
int count[] = new int[26];
boolean visited[] = new boolean[26];
Stack<Character> stack = new Stack<>();
int n = s.length();
for(int i=0;i<n;i++){
count[s.charAt(i)-'a']++;
}
for(int i=0;i<n;i++){
count[s.charAt(i)-'a']--;
if(visited[s.charAt(i)-'a']){
continue;
}
while(!stack.isEmpty() && stack.peek()>s.charAt(i) && count[stack.peek()-'a']>0){
visited[stack.peek()-'a'] = false;
stack.pop();
}
stack.push(s.charAt(i));
visited[s.charAt(i)-'a'] = true;
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
sb.insert(0, stack.peek());
stack.pop();
}
return sb.toString();
}
}class Solution {
public:
string removeDuplicateLetters(string s) {
int n = s.size();
vector<int> count(26, 0);
vector<bool> visited(26, false);
stack<char> bucket;
for(int i=0;i<n;i++){
count[s[i]-'a']++;
}
for(int i=0;i<n;i++){
count[s[i]-'a']--;
if(visited[s[i]-'a']){
continue;
}
while(!bucket.empty() && bucket.top()>s[i] && count[bucket.top()-'a']>0){
visited[bucket.top()-'a'] = false;
bucket.pop();
}
bucket.push(s[i]);
visited[s[i]-'a']=true;
}
string result = "";
while(!bucket.empty()){
result.insert(result.begin(), bucket.top());
bucket.pop();
}
return result;
}
};class Solution:
def remove_duplicate_letters(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Remove Duplicate Letters
*/
function removeDuplicateLetters(...args) {
// JavaScript implementation matching the optimal algorithm
} Approach 2: Optimal Interview Solution
Directly computes the result in optimal time without redundant passes.
Approach 2: Optimal Interview Solution Implementation
Approach 2: Optimal Interview Solution
class Solution {
public String removeDuplicateLetters(String s) {
int count[] = new int[26];
boolean visited[] = new boolean[26];
Stack<Character> stack = new Stack<>();
int n = s.length();
for(int i=0;i<n;i++){
count[s.charAt(i)-'a']++;
}
for(int i=0;i<n;i++){
count[s.charAt(i)-'a']--;
if(visited[s.charAt(i)-'a']){
continue;
}
while(!stack.isEmpty() && stack.peek()>s.charAt(i) && count[stack.peek()-'a']>0){
visited[stack.peek()-'a'] = false;
stack.pop();
}
stack.push(s.charAt(i));
visited[s.charAt(i)-'a'] = true;
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
sb.insert(0, stack.peek());
stack.pop();
}
return sb.toString();
}
}class Solution {
public:
string removeDuplicateLetters(string s) {
int n = s.size();
vector<int> count(26, 0);
vector<bool> visited(26, false);
stack<char> bucket;
for(int i=0;i<n;i++){
count[s[i]-'a']++;
}
for(int i=0;i<n;i++){
count[s[i]-'a']--;
if(visited[s[i]-'a']){
continue;
}
while(!bucket.empty() && bucket.top()>s[i] && count[bucket.top()-'a']>0){
visited[bucket.top()-'a'] = false;
bucket.pop();
}
bucket.push(s[i]);
visited[s[i]-'a']=true;
}
string result = "";
while(!bucket.empty()){
result.insert(result.begin(), bucket.top());
bucket.pop();
}
return result;
}
};class Solution:
def remove_duplicate_letters(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for Remove Duplicate Letters
*/
function removeDuplicateLetters(...args) {
// JavaScript implementation matching the optimal algorithm
} Edge Cases to Watch
- Empty or single-element input (n <= 1)
- Duplicates and boundary values near extrema
- Already sorted, reverse sorted, or uniform values
- Maximum constraint inputs to ensure no integer overflow or TLE
Master the Monotonic Stack Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Monotonic Stack pattern.
How to Recognize This Pattern
- "Smallest lexicographic subsequence with one of each char"
- Greedy stack: pop larger if a future occurrence exists
Complexity Analysis
Time Complexity
O(n)
Space Complexity
O(1)