-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ28.java
More file actions
73 lines (61 loc) · 2.81 KB
/
Q28.java
File metadata and controls
73 lines (61 loc) · 2.81 KB
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
import java.util.ArrayList;
import java.util.Arrays;
public class Q28 {
/**
* Write an algorithm to justify text. Given a sequence of words and an integer line length k,
* return a list of strings which represents each line, fully justified.
*
* More specifically, you should have as many words as possible in each line. There should be at
* least one space between each word. Pad extra spaces when necessary so that each line has exactly
* length k. Spaces should be distributed as equally as possible, with the extra spaces, if any,
* distributed starting from the left.
*
* If you can only fit one word on a line, then you should pad the right-hand side with spaces.
* Each word is guaranteed not to be longer than k.
*
* For example, given the list of words ["the", "quick", "brown", "fox", "jumps", "over",
* "the", "lazy", "dog"] and k = 16, you should return the following:
* ["the quick brown", # 1 extra space on the left
* "fox jumps over", # 2 extra spaces distributed evenly
* "the lazy dog"] # 4 extra spaces distributed evenly
*/
public static void main (String[] args) {
String[] lines = justifyLines(new String[] {"the", "quick", "brown", "fox", "jumps", "over",
"the", "lazy", "dog"}, 16);
for (String line : lines) {
System.out.println(line);
}
}
// Time complexity: O(n)
public static String[] justifyLines (String[] words, int k) {
ArrayList<String> lines = new ArrayList<>();
int wordCount = 0;
do {
int lineLength = 0;
final int wordCountBegin = wordCount;
do {
if (lineLength != 0) lineLength++; // for the space
lineLength += words[wordCount++].length();
} while (wordCount != words.length &&
lineLength + words[wordCount].length() < k);
String line = ""; // type String, anticipating short line lengths
for (int i = wordCountBegin; i < wordCount; i++) {
// place spaces
if (i != wordCountBegin) {
int leftover = (k - lineLength);
int wordsInLine = (wordCount - wordCountBegin);
int spaceCount = (int) Math.ceil(((double) leftover / wordsInLine));
for (int j = 0; j < spaceCount; j++) {
line += " ";
lineLength++;
}
// the space automatically accounted for
line += " ";
}
line += words[i];
}
lines.add(line);
} while (wordCount < words.length);
return lines.toArray(new String[lines.size()]);
}
}