FastPrepThresholded Run-Length Encoding

Thresholded Run-Length Encoding

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

You are given a nonempty lowercase string s. Process each maximal run of equal consecutive characters independently.

If a run contains at least three copies of its character, replace the entire run with the character, followed by #, followed by the run length. Leave runs of length one or two unchanged.

Return the encoded string.

After implementing the function, explain how independently processed contiguous chunks can be merged correctly when a run crosses a worker boundary.

Function

encodeLongRuns(s: String) → String

Examples

Example 1

s = "dddjjwaaaa"return = "d#3jjwa#4"

The three d characters and four a characters are encoded; the shorter runs stay unchanged.

Example 2

s = "aabbbcc"return = "aab#3cc"

Only the run of three b characters reaches the encoding threshold.

Example 3

s = "abcdef"return = "abcdef"

Every run has length one, so the string is unchanged.

Constraints

  • 1 <= s.length <= 100000.
  • s contains only lowercase English letters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String encodeLongRuns(String s) {
    // Write your code here.
}
s"dddjjwaaaa"
expected"d#3jjwa#4"
Checking account…