Thresholded Run-Length Encoding
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) → StringExamples
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.scontains only lowercase English letters.