Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Problem 8: Removing Stars From a String (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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 "b*c*d", then remove b with * to get "c*d", then remove c with * to get "d".

Example 3

  • Input: "abcd"
  • Expected Output: "abcd"
  • Description: As there is no *, the string remains the same.

Constraints:

  • 1 <= s.length <= 10<sup>5</sup>
  • s consists of lowercase English letters and stars *.
  • The operation above can be performed on s.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible