-
Notifications
You must be signed in to change notification settings - Fork 1
/
RearrangeSpacesBetweenWords.java
55 lines (49 loc) · 1.57 KB
/
RearrangeSpacesBetweenWords.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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/rearrange-spaces-between-words/
public class RearrangeSpacesBetweenWords {
private final String input;
public RearrangeSpacesBetweenWords(String input) {
this.input = input;
}
public String solution() {
List<String> words = new ArrayList<>();
int spaces = 0;
StringBuilder word = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == ' ') {
spaces++;
if (word.length() > 0) {
words.add(word.toString());
word.setLength(0);
}
} else {
word.append(c);
}
}
if (word.length() > 0) {
words.add(word.toString());
}
if (spaces == 0 || words.size() == 0) {
return input;
}
if (words.size() == 1) {
return words.get(0) + " ".repeat(spaces);
}
int actualSpaces = spaces / (words.size() - 1);
String space = " ".repeat(actualSpaces);
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.size(); i++) {
result.append(words.get(i));
if (i != words.size() - 1) {
result.append(space);
}
}
while (result.length() < input.length()) {
result.append(' ');
}
return result.toString();
}
}