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.This approach utilizes a stack data structure to keep track of characters in the string. As we iterate through the string, we push non-'*' characters onto the stack. When we encounter a '*', we pop from the stack, effectively removing the smallest character before the '*'. The stack allows us to maintain the remaining string in a way that can efficiently remove and process characters in a LIFO manner. Finally, we assemble our result from the characters remaining in the stack, which ensures the string is the lexicographically smallest possible after all required deletions.
The C code uses a dynamic character array as a stack to store non-'*' characters. As it iterates through the input string, it pushes characters onto the stack, and pops the stack when a '*' is encountered. The final stack content forms the result.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) where n is the length of the string, as each character is pushed and popped at most once.
Space Complexity: O(n) due to the usage of a stack to store characters.
This approach leverages two pointers: one pointing to the current position in the input and another building the result in place. The fundamental idea is to overwrite the unnecessary characters in the array as we build the result string. We use the second pointer to track the last added character when we encounter a '*' and need to delete. After iterating through the entire input, the section of the array up to the second pointer will hold the resulting string.
This C code utilizes two pointers within the same array to process the characters. One advances through the input while the second adjusts the positions of meaningful characters, ensuring in-place modification of the string.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) because every character is processed.
Space Complexity: O(1) as we modify the input in place.
| Approach | Complexity |
|---|---|
| Using Stack to Track Before Deletions | Time Complexity: O(n) where n is the length of the string, as each character is pushed and popped at most once. |
| Two Pointers Technique | Time Complexity: O(n) because every character is processed. |
I solved too many Leetcode problems • NeetCodeIO • 101,495 views views
Watch 9 more video solutions →Practice Lexicographically Minimum String After Removing Stars with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor