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
The return value should be a
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
"aaaaa"
Returns: "/05a"
The example stated above
"aaaa"
Returns: "aaaa"
Remember not to encode runs of length 4 or less.
"abcabcabcabcabc"
Returns: "abcabcabcabcabc"
Do not encode repeated segments of more than one character
"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}"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: "/50a"
"aaaabaaaa"
Returns: "aaaabaaaa"
"aaaabbbbccccddddeeeeffffgggghhhhiiii"
Returns: "aaaabbbbccccddddeeeeffffgggghhhhiiii"
"00000111112222233333444445555566666777778888899999"
Returns: "/050/051/052/053/054/055/056/057/058/059"
"heooooooooooo"
Returns: "he/11o"
"{([})]"
Returns: "{([})]"
"zZzZzzZzzzZzzzzZzzzzzZzzzzzzZ"
Returns: "zZzZzzZzzzZzzzzZ/05zZ/06zZ"
" "
Returns: "/42 "
"{{{{{}}}}}[[[[[]]]]]((((())))):::::;;;;;'''''+++++"
Returns: "/05{/05}/05[/05]/05(/05)/05:/05;/05'/05+"
"=====.....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
Returns: "/05=/05./40,"
""
Returns: ""
"a"
Returns: "a"
"aaaaaaaaaaaa"
Returns: "/12a"
"aaaaabvssssssdfc"
Returns: "/05abv/06sdfc"
"aaaaaaaaaaa"
Returns: "/11a"
"aaabbb"
Returns: "aaabbb"
"aaaabbbbbddddeeeeaaaa"
Returns: "aaaa/05bddddeeeeaaaa"
"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}"
"aaaanbdddddkjdddddddkd"
Returns: "aaaanb/05dkj/07dkd"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Returns: "/32a"
"aaaaaaaaaaaaaa"
Returns: "/14a"
"aasdabababeeeee{{{}}{{A}}AabB"
Returns: "aasdababab/05e{{{}}{{A}}AabB"
"aaaa"
Returns: "aaaa"
"...................................."
Returns: "/36."
"aaa"
Returns: "aaa"
"aaaaaaaaaaabbbbbbbbbbbcccccccccccccc"
Returns: "/11a/11b/14c"
""
Returns: ""
"aaaaaaaaaa"
Returns: "/10a"
"aaaaabbba"
Returns: "/05abbba"
"AAAAAAAAA"
Returns: "/09A"