Watch 10 video solutions for Lexicographically Minimum String After Removing Stars, a medium level problem involving Hash Table, String, Stack. This walkthrough by codestorywithMIK has 11,346 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s. It may contain any number of '*' characters. Your task is to remove all '*' characters.
While there is a '*', do the following operation:
'*' and the smallest non-'*' character to its left. If there are several smallest characters, you can delete any of them.Return the lexicographically smallest resulting string after removing all '*' characters.
Example 1:
Input: s = "aaba*"
Output: "aab"
Explanation:
We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest.
Example 2:
Input: s = "abc"
Output: "abc"
Explanation:
There is no '*' in the string.
Constraints:
1 <= s.length <= 105s consists only of lowercase English letters and '*'.'*' characters.Problem Overview: You receive a string containing lowercase letters and *. Each star deletes itself and one character to its left. When multiple characters exist to the left, remove the lexicographically smallest one so the final string becomes as small as possible.
Approach 1: Stack to Track Before Deletions (O(n log n) time, O(n) space)
Scan the string from left to right while tracking characters that appear before each *. Maintain a stack (or indexed storage) of characters along with their positions, and use a min‑heap or ordered structure to quickly locate the smallest available character. When you encounter a star, pop the lexicographically smallest character that appears before it and mark both the star and that character for deletion. Continue scanning until the end, then rebuild the string using only characters not removed. The key idea is greedy removal: deleting the smallest possible character before every star guarantees the remaining prefix stays lexicographically minimal. This technique commonly combines stack tracking with a heap (priority queue) to efficiently select the best character to delete.
Approach 2: Two Pointers Technique (O(n) time, O(n) space)
A more optimized method processes the string while maintaining candidate positions for each character. Instead of repeatedly searching, track indices of characters using fixed buckets (for example, 26 lists for each letter). Move through the string with a pointer and record the positions of characters you see. When a * appears, identify the smallest available letter bucket that still has a valid index and remove the most recent occurrence of that letter. This effectively simulates deletions without expensive heap operations. After processing the entire string, reconstruct the result by skipping removed indices. Because each character is inserted and removed at most once, the algorithm runs in linear time. The logic relies on greedy selection and efficient character tracking, a common pattern in string and greedy problems.
Recommended for interviews: The linear two‑pointer/bucket strategy is usually what interviewers expect. It demonstrates that you understand the greedy requirement and can optimize away repeated searches. Starting with the stack + heap idea is still valuable because it clearly models the rule "remove the smallest character before each star" and shows the reasoning before optimizing to O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Stack + Priority Queue | O(n log n) | O(n) | Straightforward greedy simulation when you want a clear mapping between stars and removed characters |
| Two Pointers / Character Buckets | O(n) | O(n) | Optimal solution for interviews and large inputs; avoids heap operations |