Statistics

Problem Statement for "RunLengthEncode"

Problem Statement

Many times, certain data file types can consist of large amounts of repeated data. For instance, images can have large runs of the same color. This can be easily compressed using a technique called run length encoding. With run length encoding, large amounts of repeated data are stored as the repeated data and the number of times to repeat it.

Create a class RunLengthEncode that contains the method encode which takes one argument:
input: a String to be encoded as described below

The return value should be a String which has been encoded with the following algorithm:

If any character is repeated more than 4 times, the entire set of repeated characters should be replaced with a slash '/', followed by a 2-digit number which is the length of the set of characters, and the character. For example, "aaaaa" would be encoded as "/05a". Runs of 4 or less characters should not be replaced since performing the encoding would not decrease the length of the string.

Definition

Class:
RunLengthEncode
Method:
encode
Parameters:
String
Returns:
String
Method signature:
String encode(String input)
(be sure your method is public)

Notes

  • Letters are case sensitive. For example "AaAaAa" cannot be encoded.
  • You may only encode repeats of a single character, repeats of multiple characters cannot be encoded. For example "ababababab" cannot be encoded as "/05ab".

Constraints

  • input will have between 0 and 50 characters, inclusive.
  • input will consist only of letters 'a' - 'z' and 'A' - 'Z', digits '0' - '9', the space character, and the characters in the following string: "{}[]():;'+=.,". (quotes are for clarity only and cannot be in the input string)

Examples

  1. "aaaaa"

    Returns: "/05a"

    The example stated above

  2. "aaaa"

    Returns: "aaaa"

    Remember not to encode runs of length 4 or less.

  3. "abcabcabcabcabc"

    Returns: "abcabcabcabcabc"

    Do not encode repeated segments of more than one character

  4. "if(a){if(b){if(c){if(d){if(e){5 deeeeeeep}}}}}"

    Returns: "if(a){if(b){if(c){if(d){if(e){5 d/07ep/05}"

  5. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    Returns: "/50a"

  6. "aaaabaaaa"

    Returns: "aaaabaaaa"

  7. "aaaabbbbccccddddeeeeffffgggghhhhiiii"

    Returns: "aaaabbbbccccddddeeeeffffgggghhhhiiii"

  8. "00000111112222233333444445555566666777778888899999"

    Returns: "/050/051/052/053/054/055/056/057/058/059"

  9. "heooooooooooo"

    Returns: "he/11o"

  10. "{([})]"

    Returns: "{([})]"

  11. "zZzZzzZzzzZzzzzZzzzzzZzzzzzzZ"

    Returns: "zZzZzzZzzzZzzzzZ/05zZ/06zZ"

  12. " "

    Returns: "/42 "

  13. "{{{{{}}}}}[[[[[]]]]]((((())))):::::;;;;;'''''+++++"

    Returns: "/05{/05}/05[/05]/05(/05)/05:/05;/05'/05+"

  14. "=====.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"

    Returns: "/05=/05./40,"

  15. ""

    Returns: ""

  16. "a"

    Returns: "a"

  17. "aaaaaaaaaaaa"

    Returns: "/12a"

  18. "aaaaabvssssssdfc"

    Returns: "/05abv/06sdfc"

  19. "aaaaaaaaaaa"

    Returns: "/11a"

  20. "aaabbb"

    Returns: "aaabbb"

  21. "aaaabbbbbddddeeeeaaaa"

    Returns: "aaaa/05bddddeeeeaaaa"

  22. "if(a){if(b){if(c){if(d){if(e){5 deeeeeeep}}}}}"

    Returns: "if(a){if(b){if(c){if(d){if(e){5 d/07ep/05}"

  23. "aaaanbdddddkjdddddddkd"

    Returns: "aaaanb/05dkj/07dkd"

  24. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    Returns: "/32a"

  25. "aaaaaaaaaaaaaa"

    Returns: "/14a"

  26. "aasdabababeeeee{{{}}{{A}}AabB"

    Returns: "aasdababab/05e{{{}}{{A}}AabB"

  27. "aaaa"

    Returns: "aaaa"

  28. "...................................."

    Returns: "/36."

  29. "aaa"

    Returns: "aaa"

  30. "aaaaaaaaaaabbbbbbbbbbbcccccccccccccc"

    Returns: "/11a/11b/14c"

  31. ""

    Returns: ""

  32. "aaaaaaaaaa"

    Returns: "/10a"

  33. "aaaaabbba"

    Returns: "/05abbba"

  34. "AAAAAAAAA"

    Returns: "/09A"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: