forked from RasPat1/practice-codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeDecode.java
71 lines (64 loc) · 2.13 KB
/
CodeDecode.java
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
class CodeDecode {
public static String code(String s) {
int sideLength = getSideLength(s);
int desiredTextLength = sideLength * sideLength;
for (int i = s.length(); i < desiredTextLength; i++) {
s += "" + (char) 11;
}
s = rotateClockwise(s);
String boxedString = "";
for (int i = 0; i < s.length() - sideLength; i += sideLength) {
boxedString += s.substring(i, i + sideLength) + "\n";
}
boxedString += s.substring(s.length() - sideLength);
return boxedString;
}
public static String decode(String s) {
s = s.replaceAll("\n", "");
s = rotateCounterClockwise(s);
s = s.replaceAll("" + (char)11, "");
return s;
}
public static int getSideLength(String s) {
return (int) Math.ceil(Math.sqrt(s.length()));
}
public static String rotateClockwise(String s) {
s = mirrorOverXeqY(s);
s = mirrorOverY(s);
return s;
}
public static String rotateCounterClockwise(String s) {
s = mirrorOverY(s);
s = mirrorOverXeqY(s);
return s;
}
public static String mirrorOverY(String s) {
int sideLength = getSideLength(s);
for (int i = 0; i < sideLength / 2; i++) {
for (int j = 0; j < sideLength; j++) {
int stringPos = sideLength * j + i;
int otherPos = sideLength * j + (sideLength - i - 1);
s = swapChars(s, stringPos, otherPos);
}
}
return s;
}
public static String mirrorOverXeqY(String s) {
int sideLength = getSideLength(s);
for (int i = 0; i < sideLength; i++) {
for (int j = 0; j < i; j++) {
// \n not part of the rotation
int stringPos = sideLength * j + i;
int otherPos = sideLength * i + j;
s = swapChars(s, stringPos, otherPos);
}
}
return s;
}
public static String swapChars(String s, int pos1, int pos2) {
char temp = s.charAt(pos1);
s = s.substring(0, pos1) + s.charAt(pos2) + s.substring(pos1 + 1);
s = s.substring(0, pos2) + temp + s.substring(pos2 + 1);
return s;
}
}