- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1592.java
38 lines (37 loc) · 1.29 KB
/
_1592.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
packagecom.fishercoder.solutions.secondthousand;
publicclass_1592 {
publicstaticclassSolution1 {
publicStringreorderSpaces(Stringtext) {
intspaceCount = 0;
for (charc : text.toCharArray()) {
if (c == ' ') {
spaceCount++;
}
}
String[] words = text.trim().split("\\s+");
if (words.length == 1) {
StringBuildersb = newStringBuilder(words[0]);
for (inti = 0; i < spaceCount; i++) {
sb.append(" ");
}
returnsb.toString();
}
inttrailingSpaces = spaceCount % (words.length - 1);
intnewSpaces = spaceCount / (words.length - 1);
StringBuildersb = newStringBuilder();
for (intj = 0; j < words.length; j++) {
sb.append(words[j]);
if (j < words.length - 1) {
for (inti = 0; i < newSpaces; i++) {
sb.append(" ");
}
} else {
for (inti = 0; i < trailingSpaces; i++) {
sb.append(" ");
}
}
}
returnsb.toString();
}
}
}