Remove Each Star and the Smallest Letter to Its Left for the Lexicographically Smallest String

Quick Overview

Delete each star in a string together with the smallest letter to its left, choosing among tied copies so that the remaining string is lexicographically smallest. It tests greedy reasoning about ties and finding the smallest remaining letter fast on inputs of up to 100,000 characters.

Remove Each Star and the Smallest Letter to Its Left for the Lexicographically Smallest String

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` made of lowercase English letters and any number of `'*'` characters. Your task is to remove all `'*'` characters. While there is a `'*'` in `s`, do the following operation: - Delete the leftmost `'*'` and the smallest non-`'*'` character to its left. If there are several copies of that smallest character, you may delete any one of them. Different choices among tied copies can leave different strings. Return the lexicographically smallest string that can remain after all stars have been removed. The input is generated so that the operation is always possible. ### Function Signature ```python def clear_stars(s: str) -> str: ... ``` ### Rules - "Smallest" means earliest in the alphabet. Only characters still present in `s` at the moment the star is processed count. - Each star removes exactly one letter, so every reachable result has the same length and the lexicographically smallest one is unique. - The result may be the empty string. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters and `'*'`. - The input is generated such that it is possible to delete all `'*'` characters. ### Examples ```text Input: s = "aaba*" Output: "aab" ``` The star must delete an `'a'`, and there are copies at indices 0, 1 and 3. Deleting index 0 or 1 leaves `"aba"`, while deleting index 3 leaves `"aab"`, which is smaller. ```text Input: s = "abc*de*f" Output: "cdef" ``` The star at index 3 sees `a`, `b`, `c` and deletes the `'a'`. The star at index 6 then sees `b`, `c`, `d`, `e` and deletes the `'b'`, which leaves `"cdef"`. ```text Input: s = "de*a*" Output: "e" ``` The first star only sees `d` and `e`, so it deletes the `'d'` even though an `'a'` comes later. The second star then sees `e` and `a` and deletes the `'a'`.

Overview: Delete each star in a string together with the smallest letter to its left, choosing among tied copies so that the remaining string is lexicographically smallest. It tests greedy reasoning about ties and finding the smallest remaining letter fast on inputs of up to 100,000 characters.

Salesforce logo
Salesforce
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s made of lowercase English letters and any number of '*' characters. Your task is to remove all '*' characters.

While there is a '*' in s, do the following operation:

  • Delete the leftmost '*' and the smallest non- '*' character to its left. If there are several copies of that smallest character, you may delete any one of them.

Different choices among tied copies can leave different strings. Return the lexicographically smallest string that can remain after all stars have been removed. The input is generated so that the operation is always possible.

Function Signature

def clear_stars(s: str) -> str:
    ...

Rules

  • "Smallest" means earliest in the alphabet. Only characters still present in s at the moment the star is processed count.
  • Each star removes exactly one letter, so every reachable result has the same length and the lexicographically smallest one is unique.
  • The result may be the empty string.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters and '*' .
  • The input is generated such that it is possible to delete all '*' characters.

Examples

Input: s = "aaba*"
Output: "aab"

The star must delete an 'a', and there are copies at indices 0, 1 and 3. Deleting index 0 or 1 leaves "aba", while deleting index 3 leaves "aab", which is smaller.

Input: s = "abc*de*f"
Output: "cdef"

The star at index 3 sees a, b, c and deletes the 'a'. The star at index 6 then sees b, c, d, e and deletes the 'b', which leaves "cdef".

Input: s = "de*a*"
Output: "e"

The first star only sees d and e, so it deletes the 'd' even though an 'a' comes later. The second star then sees e and a and deletes the 'a'.