Grokking Data Structures & Algorithms for Coding Interviews

0% completed

Solution: Removing Stars From a String

Problem Statement

Given a string s, where * represents a star. We can remove a star along with its closest non-star character to its left in a single operation.

The task is to perform as many such operations as possible until all stars have been removed and return the resultant string.

Examples

Example 1

  • Input: "abc*de*f"
  • Expected Output: "abdf"
  • Description: We remove c along with * to get "abde*f", then remove e along with * to get "abdf"

Example 2

  • Input: "a*b*c*d"
  • Expected Output: "d"
  • Description: We remove a along with * to get

.....

.....

.....

Like the course? Get enrolled and start learning!
R

reagankm

· 2 years ago

Same as the last problem. The solution talks about popping from the stack and then reversing the string to get stuff in the right order. The Java implementation sidesteps this by using a for loop to access the stack elements so no reverse is needed