-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunLength.java
More file actions
74 lines (60 loc) · 2.34 KB
/
RunLength.java
File metadata and controls
74 lines (60 loc) · 2.34 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
package RunLengthCoding;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author PlaixHax0r A.K.A Sotaya
*
*/
public class RunLength {
//pass our source into our encoder
public static String encoder(String sourc) {
StringBuilder sb = new StringBuilder();
//boolean change = true;
//iterate through each of the elements of our source
for (int i = 0; i < sourc.length(); i++) {
int lengthRun = 1;
//while there is next character and current character equals next
//we increment the length
while (i+1 < sourc.length() && sourc.charAt(i) == sourc.charAt(i+1)) {
lengthRun++;
i++;
}
//after getting all occurencies of our character, we append to the
//end of the string builder followed by the character
/*
if(sourc.charAt(0) == '1' && change){
sb.append("0");
change=false;
}*/
sb.append(lengthRun).append(sourc.charAt(i));
//sb.append(lengthRun);
}
return sb.toString();//we all know what this means
}
// Our decoder here gets the code which here means encoded source
public static String decoder(String code) {
//String builder for decoding
StringBuilder sb = new StringBuilder();
//our pattern contains all possible source alphabet that can occur
Pattern pat = Pattern.compile("[0-9]+|[a-zA-Z]+|[_]");
//our matcher takes the code and if the source letters are in our
//source alphabet, it ma
Matcher match = pat.matcher(code);
while (match.find()) {
int number = Integer.parseInt(match.group());
match.find();
//we append our match to string builder the number of times it occurs
for(int i=number;i>0;i--)
sb.append(match.group());
}
return sb.toString();//the obvious :)
}
//Main method for testing our coding and decoding
public static void main(String[] args) {
String source = "hallgatoora";
String code = "2h3l13k";
System.out.println(encoder(source));
System.out.println(decoder(code));
}
}