LeetCode Problem

How to Solve Smallest Subsequence of Distinct Characters

This problem involves finding the smallest subsequence with distinct characters in lexicographical order. The key is to use a stack-based approach with greedy techniques, ensuring no characters are repeated while maintaining the smallest sequence possible. Analyzing character occurrences and managing them in a stack ensures the correct solution efficiently.

GhostInterview Help

Need help with Smallest Subsequence of Distinct Characters without spending extra time grinding it?

GhostInterview can read Smallest Subsequence of Distinct Characters from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1081Stack-based state managementReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Stack-based state management
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem involves finding the smallest subsequence with distinct characters in lexicographical order. The key is to use a stack-based approach with greedy techniques, ensuring no characters are repeated while maintaining the smallest sequence possible. Analyzing character occurrences and managing them in a stack ensures the correct solution efficiently.

Problem Statement

You are given a string s consisting of lowercase English letters. Your task is to return the lexicographically smallest subsequence of s that contains all distinct characters from s exactly once.

The result should maintain the relative order of the characters from the original string while ensuring no character is repeated. You must optimize your solution to handle strings of length up to 1000.

Examples

Example 1

Input: s = "bcabc"

Output: "abc"

Example details omitted.

Example 2

Input: s = "cbacdcbc"

Output: "acdb"

Example details omitted.

Constraints

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.

Solution Approach

Greedy with Stack-based State Management

Use a stack to maintain the smallest lexicographical order by iterating over the string and adding characters to the stack greedily. Pop characters from the stack when a smaller character is found and the removed character can still appear later in the string.

Track Occurrences and Lexicographical Order

While iterating, keep track of the last occurrences of characters to know when it is safe to pop characters from the stack. Use a greedy approach to ensure that you add characters in the smallest order possible without violating constraints.

Efficient Stack-based Iteration

Iterate through the string while using a stack to maintain order and a bitmask or frequency counter to track characters that still need to be added. Ensure that each character is added only once, and the smallest lexicographical sequence is formed.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity is O(n), where n is the length of the string. This is due to the single pass through the string and constant-time operations for stack manipulation. The space complexity is O(k), where k is the number of distinct characters in the string, as we only store a limited number of elements in the stack and frequency table.

What Interviewers Usually Probe

  • Look for a solution that efficiently uses a stack to maintain order.
  • Ensure that the candidate tracks character occurrences and pops characters from the stack when appropriate.
  • Candidates should avoid brute-force solutions and focus on a greedy, stack-based approach.

Common Pitfalls or Variants

Common pitfalls

  • Failing to check if a character can still appear later, which can result in an incorrect subsequence.
  • Not managing the stack in lexicographical order, leading to suboptimal solutions.
  • Inefficient handling of character occurrences, which may lead to unnecessary operations and higher time complexity.

Follow-up variants

  • Handle the case where the string contains all unique characters.
  • Optimize for strings with large character repetitions.
  • Extend the solution to handle strings with uppercase letters or non-alphabetic characters.

How GhostInterview Helps

  • GhostInterview helps by suggesting a stack-based solution, guiding you through greedy decision-making to ensure an optimal subsequence.
  • The platform's hints will show you how to handle character occurrences and stack manipulations effectively.
  • By using state management and bitmasking, GhostInterview assists you in optimizing the algorithm for larger inputs.

Topic Pages

FAQ

What is the key pattern in the Smallest Subsequence of Distinct Characters problem?

The key pattern is stack-based state management, where a stack is used to maintain the lexicographically smallest subsequence by greedily adding characters.

How can I optimize my solution for strings with many repeated characters?

Optimize by using a frequency counter or bitmask to track characters that still need to be added, allowing efficient stack operations.

How does greedy decision-making affect the result?

Greedy decisions ensure that you always choose the smallest possible character that can still complete the subsequence while maintaining order.

Why is stack-based state management essential for this problem?

Stack-based state management ensures that the subsequence maintains lexicographical order while efficiently handling additions and removals of characters.

What common mistake should I avoid in this problem?

A common mistake is not managing character occurrences properly, which could lead to incorrect subsequences or inefficient solutions.

GhostInterview Solver

Need direct help with Smallest Subsequence of Distinct Characters instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Smallest Subsequence of Distinct Characters from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.