Watch 9 video solutions for Apply Operations to Make String Empty, a medium level problem involving Array, Hash Table, Sorting. This walkthrough by Aryan Mittal has 1,660 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s.
Consider performing the following operation until s becomes empty:
'a' to 'z', remove the first occurrence of that character in s (if it exists).For example, let initially s = "aabcbbca". We do the following operations:
s = "aabcbbca". The resulting string is s = "abbca".s = "abbca". The resulting string is s = "ba".s = "ba". The resulting string is s = "".Return the value of the string s right before applying the last operation. In the example above, answer is "ba".
Example 1:
Input: s = "aabcbbca" Output: "ba" Explanation: Explained in the statement.
Example 2:
Input: s = "abcd" Output: "abcd" Explanation: We do the following operation: - Remove the underlined characters s = "abcd". The resulting string is s = "". The string just before the last operation is "abcd".
Constraints:
1 <= s.length <= 5 * 105s consists only of lowercase English letters.Problem Overview: You repeatedly apply an operation on a string where the first occurrence of every distinct character is removed in a single round. The process continues until the string becomes empty. The task is to return the string that exists right before the final operation removes the remaining characters.
Approach 1: Simulating the Operations with a Queue (O(n) time, O(n) space)
This approach mimics the process exactly as described. Track the indices of each character using a queue-like structure. In every round, remove the earliest occurrence of each character and push remaining positions back for the next round. The simulation continues until no characters remain, while keeping track of the last non-empty state. The idea is straightforward and closely follows the problem statement, which makes it a good starting point during interviews. However, it performs multiple passes over the data, and managing queues for each character increases implementation overhead.
Approach 2: Direct Simulation Using Index and Mark Replacement (O(n) time, O(1) extra space)
Instead of simulating every round, observe the pattern created by the operations. A character that appears k times will survive exactly k rounds before being completely removed. Therefore, the final remaining string consists only of characters whose frequency equals the maximum frequency in the string. Count occurrences using a counting array, determine the maximum frequency, then iterate through the string to identify the last surviving occurrence for those characters. Ordering is determined by their final positions in the original string, which may require a simple sorting step based on index.
The key insight: every operation removes characters layer by layer from the front. Characters with fewer occurrences disappear earlier, while those with the highest frequency survive until the final round. That means the last non-empty string is formed by the last occurrences of the characters with maximum frequency. This transforms the problem from repeated simulation into a single pass frequency analysis using techniques common in hash table and array problems.
Recommended for interviews: Start with the simulation idea to demonstrate understanding of the operations. Then derive the frequency insight that eliminates repeated rounds. Interviewers typically expect the optimized counting-based solution because it reduces the process to a linear scan and clearly explains why only maximum-frequency characters survive.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulating the Operations with a Queue | O(n) | O(n) | When you want a direct implementation of the described operations or to verify behavior step-by-step |
| Direct Simulation Using Index and Mark Replacement | O(n) | O(1) | Best general solution using frequency counting and last-occurrence tracking |