109. String Burst

Asked in

String Burst
Given a sequence of lowercase English letters and a burstLength, repeatedly remove every contiguous group whose size is greater than or equal to burstLength.

Key details:
  • A contiguous group means consecutive equal characters.
  • In one step, remove every current contiguous group whose size is ≥ burstLength.
  • After removals, the remaining left and right parts join together, and newly adjacent equal characters may form a new group.
  • After removing all removable groups in the current step, merge the remaining parts first. Any newly formed group can only be removed in a later step.
  • Repeat this process until no more removals are possible.
  • Return the final sequence after all possible bursts are finished.

Method Signature

String arrayBurst(String sequence, int burstLength)
  • sequence is the input sequence of lowercase English letters.
  • burstLength is the minimum size of a contiguous group that must be removed.
  • Whenever a contiguous group has size ≥ burstLength, remove the whole group.
  • After each step, newly adjacent characters may form another removable group.
  • Return the final sequence after no more groups can be removed.

Constraints

  • 1 ≤ sequence.length() ≤ 10^5
  • 1 ≤ burstLength ≤ 10^5
  • sequence contains only lowercase English letters 'a' to 'z'.
  • The returned string may be empty.

Process Rules

  • A group is removable if its length is greater than or equal to burstLength.
  • All removable groups in the current sequence are removed in the same step.
  • Groups formed only after merging the remaining parts are not removed in that same step; they are checked starting from the next step.
  • After a step finishes, the sequence is checked again because chain reactions may create new removable groups.
  • If burstLength = 1, every character is removed, so the result is an empty string.
  • If no group ever reaches size burstLength, return the original sequence unchanged.

Examples

Example 1

Input:
arrayBurst(sequence = "abcdeeeeeddcbfgfhhht", burstLength = 3)

Process:
  • In the first step, eeeee and hhh are removed.
  • The remaining sequence becomes abcdddcbfgft.
  • ddd is checked only in the next step, then removed.
  • The remaining sequence becomes abccbfgft.
  • No group of size ≥ 3 remains.
Output:
"abccbfgft"

Example 2

Input:
arrayBurst(sequence = "abbba", burstLength = 3)

Process:
  • bbb is removed in the first step.
  • The remaining characters become aa.
  • aa is checked in the next step.
  • aa has size 2, which is less than 3, so it stays.
Output:
"aa"

Example 3

Input:
arrayBurst(sequence = "abcccbba", burstLength = 3)

Process:
  • ccc is removed in the first step.
  • The remaining sequence becomes abbba.
  • bbb is checked only in the next step, then removed.
  • The remaining characters become aa.
  • aa has size 2, which is less than 3, so it stays.
Output:
"aa"

Example 4

Input:
arrayBurst(sequence = "abcd", burstLength = 2)

Process:
  • No contiguous group has size ≥ 2.
  • No removal happens.
Output:
"abcd"

Example 5

Input:
arrayBurst(sequence = "abc", burstLength = 1)

Process:
  • Every character forms a group of size 1.
  • All characters are removed in one step.
Output:
""




Please use Laptop/Desktop or any other large screen to add/edit code.