Skip to main content
LeetCode 316, Medium. Topics: String, Stack, Greedy, Monotonic Stack. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

Examples

Constraints

  • 1 <= s.length <= 10^4
  • s consists of lowercase English letters.
Note: This question is the same as 1081: Smallest Subsequence of Distinct Characters.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026