FastPrepRun-Length String Compression

Run-Length String Compression

Roblox logoRoblox● EasyNEW GRADOA
Learn

Problem statement

Compress a string s using run-length encoding.

For this exercise, assume s contains only lowercase English letters. Replace each maximal consecutive run by its letter followed by the run length in ordinary base-10 digits, including the count 1 for a single letter. Concatenate the encoded runs in their original order.

Return the encoded string even when it is longer than the input. An empty input returns an empty string. Runs separated by another letter remain separate; this is consecutive-run compression, not a total frequency count.

Function

compressRuns(s: String) → String

Examples

Example 1

s = "aaabbc"return = "a3b2c1"

The consecutive runs are aaa, bb and c, with lengths 3, 2 and 1.

Example 2

s = "aba"return = "a1b1a1"

The two a runs are separated, so they cannot be combined.

Example 3

s = ""return = ""

An empty string contains no runs.

Constraints

  • 0 <= s.length <= 1000.
  • Every character is a lowercase English letter.

More Roblox problems

See Roblox hiring insights
public String compressRuns(String s) {
    // write your code here
}
s"aaabbc"
expected"a3b2c1"
Checking account…