forked from edinfazlic/CodinGame-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChuckNorris.java
41 lines (33 loc) · 1.47 KB
/
ChuckNorris.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
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Scanner;
class Solution {
public static void main(String args[]) throws UnsupportedEncodingException {
Scanner in = new Scanner(System.in);
StringBuilder binaryMessage = new StringBuilder();
String message = in.nextLine();
for (byte b : message.getBytes()) {
binaryMessage.append(String.format("%7s", Integer.toBinaryString(b)).replace(' ', '0'));
}
System.err.println("Original message: " + message);
System.err.println("Binary message : " + binaryMessage);
boolean zeroSeries = binaryMessage.charAt(0) == '0';
StringBuilder output = new StringBuilder();
int seriesLength = 1;
for (int i = 1; i < binaryMessage.length(); i++) {
if (binaryMessage.charAt(i) == binaryMessage.charAt(i - 1)) {
seriesLength++;
} else {
output.append(formatSeries(zeroSeries, seriesLength)).append(" ");
zeroSeries = !zeroSeries;
seriesLength = 1;
}
}
System.out.println(output.append(formatSeries(zeroSeries, seriesLength)));
}
private static String formatSeries(boolean zeroSeries, int seriesLength) {
char[] seriesLengthAsChars = new char[seriesLength];
Arrays.fill(seriesLengthAsChars, '0');
return (zeroSeries ? "00" : "0") + " " + new String(seriesLengthAsChars);
}
}