FastPrepDecode a Run-Length Stream

Decode a Run-Length Stream

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Decode the run-length stream in encoded. Each record consists of a positive decimal count, a colon :, and exactly one printable ASCII symbol. Records are concatenated directly.

For each record, append its symbol exactly the indicated number of times. Return all decoded symbols in record order. The empty stream decodes to the empty string.

A symbol may itself be a digit, colon, space, quotation mark or backslash. After reading the count and its colon, consume exactly one character as the symbol before parsing the next record. For example, 2:: represents two colons.

The input is one complete well-formed stream. No malformed-input error result or incremental chunk interface is required.

Function

decodeStream(encoded: String) → String

Examples

Example 1

encoded = "3:A2:01::"return = "AAA00:"

The runs produce AAA, 00, and one colon. Digits and the colon are also valid payload symbols.

Example 2

encoded = ""return = ""

An empty encoded stream contains no runs and decodes to the empty string.

Example 3

encoded = "12:x1: 1:2"return = "xxxxxxxxxxxx 2"

The decimal count 12 repeats x twelve times. The next records append a space and the digit 2.

Constraints

  • 0 <= encoded.length <= 100000.
  • Each count is an integer from 1 through 100000, written in decimal without leading zeros.
  • Each symbol has an ASCII code from 32 through 126, inclusive.
  • The input contains complete valid records, and the total decoded length is at most 100000.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public String decodeStream(String encoded) {
    // write your code here
}
encoded"3:A2:01::"
expected"AAA00:"
Checking account…