Search⌘ K

Feature #3: Balloon Splash

Explore how to develop a Balloon Splash game by using stack data structures to remove k consecutive identical balloons in a string representation. Understand the algorithm to repeatedly splash balloons and handle falling elements. Learn the time and space complexity of this solution to solidify your problem-solving skills in coding interviews.

Description

We have to develop the game named Balloon Splash. Assume that there are multiple colored balloons, and we have to shoot a column of balloons of the same color. We can only splash k consecutive balloons of the same color. Once a set of k consecutive balloons is splashed, any balloons above it will fall to replace them. We will keep shooting until it is not possible to shoot k consecutive balloons of the same color. In the end, there will not be any k consecutive balloons left. For this problem, our input will be in the form of the English alphabet. Each letter will represent a unique colored balloon.

Let’s go through the following illustration to understand this problem:

Solution

We are given a string str and an integer k, where the k consecutive letters that are identical can be eliminated from the string str. After removing the k consecutive occurrences of the same letter, we have to concatenate the remaining substrings together.

Here is how we will implement this feature:

...