Removing Stars From a String [Medium]

20 Sep 2026 2 min read
0
Yes. This is a good **Solution 2** because it avoids using `deleteCharAt()` and processes the string from right to left using a counter.

Solution

Solution 1: Stack

This solution uses a Stack to keep track of the characters that have not been removed. When we encounter a normal character, we add it to the stack. When we encounter a *, we remove the most recently added character from the stack.

The stack follows the Last In, First Out (LIFO) principle, which makes it suitable because each * removes the closest non-star character to its left.
class Solution {
    public String removeStars(String s) {
        StringBuilder stack = new StringBuilder();

        for (char c : s.toCharArray()) {
            if (c == '*') {
                stack.deleteCharAt(stack.length() - 1);
            } else {
                stack.append(c);
            }
        }

        return stack.toString();
    }
}

Solution 2: Reverse Traversal

This approach traverses the string from right to left. We maintain a star 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

Both solutions process each character once, so the time complexity is O(n), where n is the length of the string.

Both solutions use O(n) extra space for the resulting StringBuilder.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion