Leetcode 2390: Removing Stars From a String

grid47
grid47
Exploring patterns and algorithms
Mar 13, 2024 4 min read

You are given a string s that contains lowercase English letters and stars (*). In each operation, you can choose a star in s, remove the closest non-star character to its left, and remove the star itself. Perform this operation until all stars are removed and return the resulting string.
Problem
Approach
Steps
Complexity
Input: The input consists of a string `s` that contains lowercase English letters and stars (`*`).
Example: Input: s = 'abc*def*gh*'
Constraints:
• 1 <= s.length <= 10^5
• s consists of lowercase English letters and stars `*`.
• The operation above can be performed on s.
Output: Return the string after all stars have been removed by performing the operation as described.
Example: Output: 'abgh'
Constraints:
Goal: The goal is to remove the closest character to each star from left to right until all stars are removed.
Steps:
• 1. Traverse the string and for each star, remove the closest non-star character to its left.
• 2. After each removal, adjust the string accordingly and continue the operation.
• 3. Repeat the process until all stars are removed.
Goal: The string can have up to 100,000 characters and consists of lowercase English letters and stars.
Steps:
• The operation should be performed on strings of length up to 10^5.
Assumptions:
• There will always be a non-star character to remove before each star.
Input: Input: s = 'abc*def*gh*'
Explanation: In the first operation, remove 'c' before the first star, resulting in 'ab*def*gh*'. Then remove 'f' before the second star, resulting in 'abdef*gh*'. Finally, remove 'g' before the last star, leaving 'abgh'.

Input: Input: s = 'abc*****'
Explanation: In this case, all characters are removed sequentially one by one before each star, resulting in an empty string.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus