forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (33 loc) · 1.08 KB
/
Solution.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Oleg Cherednik
* @since 31.10.2018
*/
public class Solution {
public static void main(String... args) {
List<String> res = splitIntoWords("the quick brown fox jumps over the lazy dog", 10);
System.out.println(res != null ? Arrays.toString(res.toArray(new String[0])) : null);
}
public static List<String> splitIntoWords(String str, int k) {
String[] words = str.split("\\s+");
List<String> res = new ArrayList<>(words.length);
StringBuilder buf = null;
for (String word : words) {
if (word.length() > k)
return null;
if (buf == null)
buf = new StringBuilder(word);
else if (buf.length() + word.length() + 1 <= k)
buf.append(' ').append(word);
else {
res.add(buf.toString());
buf = new StringBuilder(word);
}
}
if (buf != null)
res.add(buf.toString());
return res;
}
}