Back to Month
MEDIUM 19 Jul 2026 View on LeetCode

1081. Smallest Subsequence of Distinct Characters

</> Solution

import java.util.*;
class Solution {
    public String smallestSubsequence(String s) {
        int[] last = new int[26];
        boolean[] used = new boolean[26];
        for (int i = 0; i < s.length(); i++) {
            last[s.charAt(i) - 'a'] = i;
        }
        Deque<Character> stack = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (used[c - 'a']) continue;
            while (!stack.isEmpty()
                    && stack.peekLast() > c
                    && last[stack.peekLast() - 'a'] > i) {
                used[stack.pollLast() - 'a'] = false;
            }
            stack.offerLast(c);
            used[c - 'a'] = true;
        }
        StringBuilder ans = new StringBuilder();
        while (!stack.isEmpty()) {
            ans.append(stack.pollFirst());
        }
        return ans.toString();
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(1)

TOPICS

Greedy Monotonic Stack Stack String