FastPrepOne-Dimensional Candy Crush

One-Dimensional Candy Crush

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a string s and an integer threshold k, repeatedly remove any maximal contiguous run of at least k equal characters.

Process runs from left to right. After a removal, the characters on its two sides become adjacent and may form a new qualifying run. Continue until no qualifying run remains, then return the remaining string.

Function

crushCandy(s: String, k: int) → String

Examples

Example 1

s = "aaabbbc"k = 3return = "c"

Remove aaa, then remove the newly exposed run bbb.

Example 2

s = "aabbbacd"k = 3return = "cd"

Removing bbb joins two and one a into aaa, which is removed next.

Example 3

s = "aabbccddeeedcba"k = 3return = ""

Each removal exposes the next three-character run until the string is empty.

Constraints

  • 0 <= s.length <= 10^5.
  • 2 <= k <= 10^5.
  • s contains lowercase English letters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String crushCandy(String s, int k) {
  // Write your code here.
}
s"aaabbbc"
k3
expected"c"
Checking account…