Smallest Subsequence Keeping Each Letter Once

Quick Overview

Smallest Subsequence Keeping Each Letter Once. Return the lexicographically smallest subsequence of s that includes each distinct letter appearing in s exactly once.

Smallest Subsequence Keeping Each Letter Once

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the lexicographically smallest subsequence of s that includes each distinct letter appearing in s exactly once. Character order must follow the input, and no distinct letter may be omitted. ```cpp class Solution { public: string smallestSubsequence(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "bbaa" Output: "ba" Explanation: Both letters are required, but every b precedes every a, so ab is not a subsequence. ``` **Example 2:** ```text Input: s = "dcabdc" Output: "abdc" Explanation: The suffix abdc includes each of a,b,c,d once and begins with the smallest feasible first letter. ``` ### Constraints - The input has 1 to 1000 letters. - Only lowercase English letters occur. ### Contract considerations All distinct letters must appear, and lexicographic minimization makes the answer deterministic. ```hint Read the boundary condition Which requirements must hold together: input order, every distinct letter, and exactly one copy of each? ``` ### Boundary reminders All distinct letters must appear, and lexicographic minimization makes the answer deterministic.

Overview: Smallest Subsequence Keeping Each Letter Once. Return the lexicographically smallest subsequence of s that includes each distinct letter appearing in s exactly once.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the lexicographically smallest subsequence of s that includes each distinct letter appearing in s exactly once. Character order must follow the input, and no distinct letter may be omitted.

class Solution {
public:
    string smallestSubsequence(string s);
};

Examples

Example 1:

Input: s = "bbaa"
Output: "ba"
Explanation: Both letters are required, but every b precedes every a, so ab is not a subsequence.

Example 2:

Input: s = "dcabdc"
Output: "abdc"
Explanation: The suffix abdc includes each of a,b,c,d once and begins with the smallest feasible first letter.

Constraints

  • The input has 1 to 1000 letters.
  • Only lowercase English letters occur.

Contract considerations

All distinct letters must appear, and lexicographic minimization makes the answer deterministic.

Boundary reminders

All distinct letters must appear, and lexicographic minimization makes the answer deterministic.