PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Remove repeated lowercase letters so each distinct character appears once in the lexicographically smallest valid subsequence. A monotonic stack, remaining-occurrence information, and a membership set produce a linear-time solution without enumerating subsequences.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Remove Duplicate Letters Lexicographically

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Remove Duplicate Letters Lexicographically Given a lowercase string, remove repeated occurrences so that every distinct letter appears exactly once. The returned string must be a subsequence of the input and must be the lexicographically smallest result among all subsequences that contain each distinct letter exactly once. ### Function Signature ```python def remove_duplicate_letters(text: str) -> str: ... ``` ### Input - `text` contains only lowercase English letters. ### Output Return the lexicographically smallest valid subsequence containing each distinct input letter exactly once. ### Constraints - `1 <= len(text) <= 100_000` ### Examples ```text Input: "bcabc" Output: "abc" Input: "cbacdcbc" Output: "acdb" Input: "aaaa" Output: "a" ``` ### Clarifications - Relative order must be preserved because the result is a subsequence. - Lexicographic comparison uses normal lowercase letter order. - A solution should handle the largest input without enumerating subsequences.

Quick Answer: Remove repeated lowercase letters so each distinct character appears once in the lexicographically smallest valid subsequence. A monotonic stack, remaining-occurrence information, and a membership set produce a linear-time solution without enumerating subsequences.

Remove repeated occurrences from a lowercase string so every distinct letter appears exactly once. The result must remain a subsequence and be lexicographically smallest among all valid subsequences.

Constraints

  • The input contains only lowercase English letters.
  • Every distinct input letter appears exactly once in the result.
  • The result must be a subsequence of the input.
  • The input length is at least one and at most 100,000.
  • Do not enumerate subsequences.

Examples

Input: ("bcabc",)

Expected Output: 'abc'

Explanation: The later b and c allow the leading larger letters to be replaced.

Input: ("cbacdcbc",)

Expected Output: 'acdb'

Explanation: The canonical example preserves feasibility while minimizing the prefix.

Hints

  1. Track the last position of each character.
  2. Maintain a stack of characters already selected.
  3. Pop a larger stack top only when it appears again later.
Last updated: Jul 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Elements Occurring More Than n/3 Times in a Sorted Array - Bytedance (medium)
  • Least Frequently Used (LFU) Cache - Bytedance (hard)
  • Course Schedule Feasibility - Bytedance (hard)
  • Find the Best Word for a Query Suffix - Bytedance (hard)