Skip to main content
Back to problems
Leetcode
Medium
Strings
Stacks
Greedy
Hash Maps
Google
Meta
1081. Smallest Subsequence of Distinct Characters

Return the lexicographically smallest subsequence that contains every distinct character of the input string exactly once.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

Smallest Subsequence of Distinct Characters

gfg
Primary
Problem Statement

Problem

Given a string s, build a subsequence that:

  1. contains every distinct character that appears in s, exactly once
  2. is a subsequence of s
  3. is the lexicographically smallest possible among all valid subsequences

A subsequence keeps the original relative order of characters, but you may remove any number of characters.

Goal

Choose characters so that every distinct letter appears once, while making the resulting string as small as possible in lexicographic order.

Input Format

  • A single string s consisting of lowercase English letters.

Output Format

  • Return the lexicographically smallest valid subsequence as a string.

Constraints

  • 1 <= s.length <= $10^{4}$
  • s contains only lowercase English letters
  • The answer must include each distinct character from s exactly once
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "bcabc"

Output

"abc"

Explanation

The distinct characters are {a, b, c}. Among all subsequences containing each once, "abc" is lexicographically smallest.

Example 2

Input

s = "cbacdcbc"

Output

"acdb"

Explanation

One valid subsequence is "acdb". It contains each distinct character exactly once and is lexicographically smaller than other valid choices such as "adbc".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.