-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashatizeIt.java
39 lines (33 loc) · 1.07 KB
/
DashatizeIt.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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.util.Arrays;
public class DashatizeIt {
private final int input;
public DashatizeIt(int input) {
this.input = input;
}
public String solution() {
boolean previousOdd = false;
StringBuilder result = new StringBuilder();
int[] digits = Arrays
.stream(String.valueOf(input).split(""))
.filter(s -> Character.isDigit(s.charAt(0)))
.mapToInt(Integer::valueOf)
.toArray();
for (int i = 0; i < digits.length; i++) {
int j = digits[i];
if (j % 2 != 0) {
result.append("-");
result.append(j);
previousOdd = true;
} else {
if (previousOdd) {
previousOdd = false;
result.append("-");
}
result.append(j);
}
}
String output = result.toString();
return output.startsWith("-") ? output.substring(1) : output;
}
}