Problem · String

Reversible Run-Length Encoding

Learn this problem
MediumZillow logoZillowFULLTIMEPHONE SCREEN

Problem statement

Process ENCODE and DECODE rows over printable ASCII strings. Return one transformed string per row.

Encode every maximal run as count#asciiCode;, where count is the positive decimal run length and asciiCode is the character's decimal ASCII code. Concatenate the run records without separators beyond the shown # and ;. The empty string encodes to the empty string.

A row is either ["ENCODE", plainText] or ["DECODE", encodedText]. Every decode input is a valid string produced by this encoding.

Function

transformRuns(operations: String[][]) → String[]

Examples

Example 1

operations = [["ENCODE","aa11!!"],["DECODE","2#97;2#49;2#33;"],["ENCODE","A;#"]]return = ["2#97;2#49;2#33;","aa11!!","1#65;1#59;1#35;"]

Digits and delimiter characters are represented by their ASCII codes, so decoding is unambiguous.

Example 2

operations = [["ENCODE",""],["DECODE",""],["ENCODE","$$$$"]]return = ["","","4#36;"]

Empty text remains empty, while four dollar signs form one run with ASCII code 36.

Constraints

  • 1 <= operations.length <= 100000.
  • Plain text has at most 100000 printable ASCII characters.
  • The total decoded length across the batch is at most 1000000.
  • Every DECODE input is a valid encoding generated by the documented format.
drafts saved locally
public String[] transformRuns(String[][] operations) {
    // Write your code here.
}
operations[["ENCODE","aa11!!"],["DECODE","2#97;2#49;2#33;"],["ENCODE","A;#"]]
expected["2#97;2#49;2#33;", "aa11!!", "1#65;1#59;1#35;"]
checking account