-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertDashes.java
35 lines (30 loc) · 1.02 KB
/
InsertDashes.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
package com.smlnskgmail.jaman.codewarsjava.kyu7;
// https://www.codewars.com/kata/55960bbb182094bc4800007b
public class InsertDashes {
private final int input;
public InsertDashes(int input) {
this.input = input;
}
public String solution() {
StringBuilder result = new StringBuilder();
String[] digits = String.valueOf(input).split("");
for (int i = 0; i < digits.length; i++) {
int digit = Integer.parseInt(digits[i]);
if (digit % 2 != 0) {
if (i - 1 >= 0) {
int additionalDigit = Integer.parseInt(digits[i - 1]);
if (additionalDigit % 2 != 0) {
result.append("-").append(digit);
} else {
result.append(digit);
}
} else {
result.append(digit);
}
} else {
result.append(digit);
}
}
return result.toString();
}
}