AlgoMaster Logo

Removing Stars From a String

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force Using Stack

We can solve this problem using a stack to handle the character additions and star removals efficiently.

Intuition:

  • Traverse the string character by character.
  • Use a stack to keep track of characters that have been added.
  • When encountering a star ('*'), pop the top element from the stack if it's not empty. This simulates the action of the star removing the preceding character.
  • Continue this process until the entire string is traversed.
  • Finally, convert the stack back to a string which represents the transformed version of the original string without the stars and the characters removed by them.

Steps:

  1. Initialize a stack to hold characters.
  2. Traverse each character in the string.
    • If the character is not '*', push it onto the stack.
    • If the character is '*', pop the top character from the stack if the stack is not empty.
  3. Convert the stack to a string and return it.

Code:

2. Optimized In-place Modification

We can enhance the efficiency by using a similar logic to sweep through the array, using in-place modifications to reduce the auxiliary space.

Intuition:

  • Instead of using a data structure like stack, use a characteristic of a string where stars remove characters.
  • Traverse the string while maintaining an index to record the position at which the current non-star character should be placed.
  • For each non-star character, place it at the current index and increment the index.
  • For a star ('*'), decrement the index to account for removing the previous character.

Steps:

  1. Convert the string into a character array.
  2. Use variable index to track the write position for valid characters.
  3. Iterate over the character array:
    • For non-star characters, place the character at the current index and then increment index.
    • For a star, decrement index.
  4. Construct the resultant string using the valid portion of the character array up to index.

Code: