TopcoderArchive
Archive
Algorithm problem

StringDecompressor

Problem statement, definition, constraints, and public examples.

Problem Statement

One type of compression is run length encoding. In this case, a character may be preceded by an integer N, which indicates that the character should be repeated N times. For example, the String "3ABBC10D" would be decompressed as "AAABBCDDDDDDDDDD". A character that is not immediately preceded by an integer is repeated just once. You will be given a String that has been compressed and you are to decompress it and return the result.

Definition

Class:
StringDecompressor
Method:
decompress
Parameters:
String
Returns:
String
Method signature:
String decompress(String s)
(be sure your method is public)

Constraints

  • s will contain between 0 and 50 uppercase letters ('A'-'Z') and digits ('0'-'9').
  • The length of the decompressed string will be at most 1,000 characters.
  • The last character of s will not be a digit.

Examples

  1. "3ABBC10D" 
    Returns: "AAABBCDDDDDDDDDD"
  2. ""
    Returns: ""
  3. "0AA001B100C"
    Returns: "ABCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
    Note that the first A is preceded by a 0, and hence is repeated 0 times.
  4. "X02V02M0002QVP2D02OA2N2O0Y2O3AORXTUH0KXU4V0PE04V"
    Returns: "XVVMMQQVPDDOOANNOOOOAAAORXTUHXUVVVVEVVVV"
Back to all problems