|
|
@@ -1,44 +1,44 @@
|
|
1
|
1
|
import java.util.ArrayList;
|
|
2
|
2
|
import java.util.List;
|
|
3
|
3
|
|
|
4
|
|
-public class Atbash {
|
|
|
4
|
+class Atbash {
|
|
5
|
5
|
|
|
6
|
6
|
private static final int GROUP_SIZE = 5;
|
|
7
|
7
|
private static final String PLAIN = "abcdefghijklmnopqrstuvwxyz";
|
|
8
|
8
|
private static final String CIPHER = "zyxwvutsrqponmlkjihgfedcba";
|
|
9
|
9
|
|
|
10
|
|
- public String encode(String input) {
|
|
|
10
|
+ String encode(String input) {
|
|
11
|
11
|
String encoded = stripInvalidCharacters(input).toLowerCase();
|
|
12
|
|
- String cyphered = "";
|
|
|
12
|
+ StringBuilder cyphered = new StringBuilder(input.length());
|
|
13
|
13
|
|
|
14
|
14
|
for (char c : encoded.toCharArray()) {
|
|
15
|
|
- cyphered += applyCipher(c);
|
|
|
15
|
+ cyphered.append(applyCipher(c));
|
|
16
|
16
|
}
|
|
17
|
17
|
|
|
18
|
|
- return splitIntoFiveLetterWords(cyphered);
|
|
|
18
|
+ return splitIntoFiveLetterWords(cyphered.toString());
|
|
19
|
19
|
}
|
|
20
|
20
|
|
|
21
|
|
- public String decode(String input) {
|
|
|
21
|
+ String decode(String input) {
|
|
22
|
22
|
String encoded = stripInvalidCharacters(input).toLowerCase();
|
|
23
|
|
- String deciphered = "";
|
|
|
23
|
+ StringBuilder deciphered = new StringBuilder(input.length());
|
|
24
|
24
|
|
|
25
|
25
|
for (char c : encoded.toCharArray()) {
|
|
26
|
|
- deciphered += applyCipher(c);
|
|
|
26
|
+ deciphered.append(applyCipher(c));
|
|
27
|
27
|
}
|
|
28
|
28
|
|
|
29
|
|
- return deciphered;
|
|
|
29
|
+ return deciphered.toString();
|
|
30
|
30
|
}
|
|
31
|
31
|
|
|
32
|
32
|
private String stripInvalidCharacters(String input) {
|
|
33
|
|
- String filteredValue = "";
|
|
|
33
|
+ StringBuilder filteredValue = new StringBuilder(input.length());
|
|
34
|
34
|
|
|
35
|
35
|
for (char c : input.toCharArray()) {
|
|
36
|
36
|
if (Character.isLetterOrDigit(c)) {
|
|
37
|
|
- filteredValue += c;
|
|
|
37
|
+ filteredValue.append(c);
|
|
38
|
38
|
}
|
|
39
|
39
|
}
|
|
40
|
40
|
|
|
41
|
|
- return filteredValue;
|
|
|
41
|
+ return filteredValue.toString();
|
|
42
|
42
|
}
|
|
43
|
43
|
|
|
44
|
44
|
private char applyCipher(char input) {
|