-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathLineWrap.java
84 lines (76 loc) · 2.63 KB
/
LineWrap.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.company.google;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LineWrap {
/*
Given a string that can be a sentence, a paragraph or a whole book, and a line limit, provide a list of text lines that wrap at that limit.
" Hello world. This is a Google interview, good luck ! ", 12
Result:
"Hello world."
"This is a"
"Google"
"Interview,"
"Good luck!"
*/
public static void main(String[] args) {
final List<String> input =
wrapInput(" Hello world. This is Google big-interview, good luck!", 12);
input.forEach(System.out::println);
}
public static List<String> wrapInput(final String input, final int limit) {
final String trimmedInput = input.trim();
final int length = trimmedInput.length();
if (length == 0 || limit < 1) {
return Collections.EMPTY_LIST;
}
final List<String> result = new ArrayList<>();
final String[] words = input.split(" ");
/**
* We'll split by spaces
* Hello
* world.
* This
* is
* a
* Google
* interview,
* good
* luck!
*/
for (int i = 0; i < words.length; ) {
String word = words[i];
if (word.trim().length() == 0) {
i++;
continue;
}
int lengthOfLine = word.length();
if (word.length() > limit) {
result.add(word.substring(0, limit) + "--word-in-new-line---");
result.add(word.substring(limit));
i++;
} else {
if (word.length() == limit) {
result.add(word.trim());
i++;
} else {
// We can add more words under us.
final List<String> newWords = new ArrayList<>();
int j = i + 1;
for (; j < words.length; j++) {
if (lengthOfLine + words[i].length() + 1 < limit) {
newWords.add(words[j]);
lengthOfLine += words[j].length() + 1;
} else {
break;
}
}
String line = word + " " + String.join(" ", newWords);
result.add(line.trim());
i = j;
}
}
}
return result;
}
}