-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunLengthEncoder.java
More file actions
141 lines (128 loc) · 5.07 KB
/
Copy pathRunLengthEncoder.java
File metadata and controls
141 lines (128 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public class RunLengthEncoder {
/**
* Encodes a string using run-length encoding.
*
* Consecutive repeated characters are replaced by the count followed by
* the character. A count of 1 is still written explicitly.
*
* Examples:
* "aaabbc" -> "3a2b1c"
* "abcd" -> "1a1b1c1d"
* "aabbcc" -> "2a2b2c"
* "aaaaaaaaaaaab" -> "12a1b" (counts can be more than one digit)
* "" -> ""
*
* @param input the string to encode (non-null)
* @return the run-length encoded string
*/
public static String encode(String input) {
String result = "";
for(int i=0;i<input.length();i++) {
char c = input.charAt(i);
if (String.valueOf(c).matches("[A-Za-z]")) {
int count = 1;
while(i<input.length()-1) {
if (input.charAt(i + 1) == c) {
count++;
i++;
} else {
break;
}
}
result += String.valueOf(count) + String.valueOf(c);
}
}
return result;
}
/**
* Decodes a run-length encoded string back to its original form.
*
* Each token is a number followed by a character. The number may be
* more than one digit.
*
* Examples:
* "3a2b1c" -> "aaabbc"
* "1a1b1c1d" -> "abcd"
* "12a1b" -> "aaaaaaaaaaaab"
* "" -> ""
*
* @param input the encoded string to decode (non-null)
* @return the decoded original string
*/
public static String decode(String input) {
// Define result
// iterate over input
// if it's a number, look at subsequent chars to see if they are also numbers until you hit a letter
// put the digits together and parse them, then add that many of the letter to the result string
String result = "";
for(int i=0;i<input.length();i++) {
char c = input.charAt(i);
String num = String.valueOf(c);
if (num.matches("[0-9]")) {
while(i<input.length()-1) {
String nextC = String.valueOf(input.charAt(i+1));
if (nextC.matches("[0-9]")) {
num += nextC;
i++;
} else if (nextC.matches("[a-zA-Z]")) {
result += nextC.repeat(Integer.parseInt(num));
i++;
break;
}
}
}
}
return result;
}
// -------------------------------------------------------------------------
// Test runner
// -------------------------------------------------------------------------
public static void main(String[] args) {
int passed = 0, failed = 0;
Object[][] encodeTests = {
// { input, expected }
{ "aaabbc", "3a2b1c" },
{ "abcd", "1a1b1c1d" },
{ "aabbcc", "2a2b2c" },
{ "aaaaaaaaaaaab", "12a1b" }, // count > 9
{ "a", "1a" }, // single char
{ "aaa", "3a" }, // all same
{ "", "" }, // empty
};
System.out.println("=== encode ===");
for (Object[] test : encodeTests) {
String input = (String) test[0];
String expected = (String) test[1];
String actual = encode(input);
boolean ok = expected.equals(actual);
System.out.printf(" %-20s -> %-15s %s%n",
"\"" + input + "\"",
actual == null ? "null" : "\"" + actual + "\"",
ok ? "PASS" : "FAIL (expected \"" + expected + "\")");
if (ok) passed++; else failed++;
}
Object[][] decodeTests = {
// { input, expected }
{ "3a2b1c", "aaabbc" },
{ "1a1b1c1d", "abcd" },
{ "2a2b2c", "aabbcc" },
{ "12a1b", "aaaaaaaaaaaab" }, // count > 9
{ "1a", "a" }, // single char
{ "3a", "aaa" }, // all same
{ "", "" }, // empty
};
System.out.println("=== decode ===");
for (Object[] test : decodeTests) {
String input = (String) test[0];
String expected = (String) test[1];
String actual = decode(input);
boolean ok = expected.equals(actual);
System.out.printf(" %-20s -> %-25s %s%n",
"\"" + input + "\"",
actual == null ? "null" : "\"" + actual + "\"",
ok ? "PASS" : "FAIL (expected \"" + expected + "\")");
if (ok) passed++; else failed++;
}
System.out.printf("%nResults: %d passed, %d failed%n", passed, failed);
}
}