* along with the closest non-star character to its left.
Problem
Given a string s containing lowercase English letters and* characters, remove all * characters.
For every
*, remove the closest non-star character to its left along with the * itself.Return the resulting string after all stars have been removed.
Example(s)
Consider the following examples to understand the expected input and output.Example 1
Input
s = "back**end*ml"
Output
"baenml"
Example 2
Input
s = "erase*****"
Output
""
Solution
There are three approaches to solve this problem.The first approach uses a Stack with reverse traversal, the second approach uses a StringBuilder that behaves like a stack, and the third approach traverses the string from right to left using a counter.
Approach 1: Stack with Reverse Traversal
This approach uses a Stack to process the string from right to left. First, we push every character of the string into the stack.We maintain a
stars counter to keep track of the number of * characters that need to remove characters.
When the top of the stack is a
*, we remove it and increment stars. When the top is a normal character and stars > 0, we remove that character and decrement stars. Otherwise, we add the character to the result.
Since the characters are collected from right to left, we reverse the StringBuilder before returning the result.
class Solution {
public String removeStars(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
StringBuilder sb = new StringBuilder();
int stars = 0;
while (!stack.isEmpty()) {
if (stack.peek() == '*') {
stack.pop();
stars++;
} else {
if (stars > 0) {
stack.pop();
stars--;
} else {
sb.append(stack.pop());
}
}
}
return sb.reverse().toString();
}
}
Complexity
We push and pop each character at most once, so the time complexity isO(n).
The Stack stores all characters and the StringBuilder stores the result, so the extra space complexity is
O(n).
Approach 2: StringBuilder
This approach uses a StringBuilder to maintain the characters that have not been removed. When we encounter a normal character, we append it to the StringBuilder. When we encounter a*, we remove the most recently added character.
The StringBuilder behaves like a stack here because the most recently added character is the first one removed. This follows the Last In, First Out (LIFO) principle, which matches the requirement that each
* removes the closest non-star character to its left.
class Solution {
public String removeStars(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (c == '*') {
sb.deleteCharAt(sb.length() - 1);
} else {
sb.append(c);
}
}
return sb.toString();
}
}
Complexity
We process each character once, so the time complexity isO(n), where n is the length of the string.
The StringBuilder stores the remaining characters, so the extra space complexity is
O(n).
Approach 3: Reverse Traversal
This approach traverses the string from right to left. We maintain astar counter representing the number of * characters that still need to remove characters.
When we encounter a
*, we increment star. When we encounter a normal character and star > 0, that character is removed, so we decrement star. Otherwise, the character is added to the result.
Since the characters are collected from right to left, we reverse the StringBuilder before returning the result.
class Solution {
public String removeStars(String s) {
char[] arr = s.toCharArray();
StringBuilder sb = new StringBuilder();
int star = 0;
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == '*') {
star++;
} else if (star > 0) {
star--;
} else {
sb.append(arr[i]);
}
}
return sb.reverse().toString();
}
}
Complexity
We traverse the string once, so the time complexity isO(n). The StringBuilder stores the remaining characters, so the extra space complexity is O(n).
Comments
No comments yet. Ask a question or share what helped.