FastPrepRun-Length Decoding

Run-Length Decoding

Apple logoApple● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Decode a run-length encoded string encoded. Each token is one lowercase English letter followed by an optional positive decimal count. A missing count means 1. Emit the letter that many times and concatenate the runs in order.

For example, a5b3c expands to aaaaabbbc. Counts belong to the letter immediately before them and can have multiple digits. The empty string decodes to the empty string.

Function

decodeRuns(encoded: String) → String

Examples

Example 1

encoded = "a5b3c"return = "aaaaabbbc"

The original example uses postfix counts and an omitted count of one.

Example 2

encoded = ""return = ""

Empty encoded input expands to an empty string.

Example 3

encoded = "x12yz2"return = "xxxxxxxxxxxxyzz"

Multi-digit counts and consecutive omitted counts.

Constraints

  • The input is valid and contains only lowercase English letters and decimal digits.
  • Every count is positive, has no leading zero, and is at most 4096.
  • 0 <= encoded.length <= 4096.
  • The decoded output has at most 4096 characters.

More Apple problems

See Apple hiring insights
public String decodeRuns(String encoded) {
    // write your code here
}
encoded"a5b3c"
expected"aaaaabbbc"
Checking account…