-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathText Justification.java
62 lines (56 loc) · 2.28 KB
/
Text Justification.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
// Runtime: 4 ms (Top 15.97%) | Memory: 42.1 MB (Top 76.75%)
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> unBalanced = new ArrayList<>();
List<String> balanced = new ArrayList<>();
int numSpaces = 0;
StringBuffer sb = new StringBuffer();
//Following code creates a list of unbalanced lines by appending words and 1 space between them
for(String word : words){
if(sb.length() == 0){
sb.append(word);
}else{
if(sb.length() + 1 + word.length() <= maxWidth){
sb.append(" "+word);
}else{
unBalanced.add(sb.toString());
sb = new StringBuffer(word);
}
}
}
if(sb.length() >0){
unBalanced.add(sb.toString());
}
for(int j = 0; j < unBalanced.size(); j++){
String line = unBalanced.get(j);
numSpaces = maxWidth - line.length();
StringBuffer lineB = new StringBuffer(line);
//This if block handles either last line or the scenario where in there's only one word in any sentence and hence no spaces
if(j == unBalanced.size()-1 || !line.contains(" ")){
int tempSpaces = maxWidth - lineB.length();
while(tempSpaces > 0){
lineB.append(" ");
tempSpaces --;
}
balanced.add(lineB.toString());
continue;
};
// The following block checks for each character and appends 1 space during each loop
//If there are still spaces left at the end of the String, again start from beggining and append spaces after each word
while(numSpaces > 0){
int i = 0;
while(i < lineB.length() - 1){
if( lineB.charAt(i) == ' ' && lineB.charAt(i+1) != ' '){
lineB.insert(i+1, ' ');
i++;
numSpaces --;
if(numSpaces == 0) break;
}
i++;
}
}
balanced.add(lineB.toString());
}
return balanced;
}
}