-
Notifications
You must be signed in to change notification settings - Fork 0
/
68. Text Justification
55 lines (54 loc) · 1.48 KB
/
68. Text Justification
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
class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
int n = words.length;
int i=0;
List<String> ans = new ArrayList<>();
while(i<n){
int l =0;
int space=0;
int k=i;
while(k<n && l+words[k].length()+space<=maxWidth){
l+=words[k++].length()+space;
space=1;
}
ans.add(getText(i, k, words, maxWidth, k==n));
i=k;
}
return ans;
}
String getText(int i, int k, String[] words, int maxWidth, boolean isLast){
int w = 0;
int count = k-i-1;
int t =i;
StringBuilder sb = new StringBuilder();
if(count==0 ) {
sb.append(words[t]);
sb.append(" ".repeat(maxWidth-words[t].length()));
return sb.toString();
}
while(t<k){
w+=words[t++].length();
}
int space = maxWidth-w;
int btw = space/count;
int rem = space%count;
t= i;
while(t<k){
sb.append(words[t++]);
if(isLast){
if(t!=k){
sb.append(" ");
space--;
}else{
sb.append(" ".repeat(space));
}
}else{
if(t!=k){
sb.append(" ".repeat(btw));
}
if(rem-- > 0) sb.append(" ");
}
}
return sb.toString();
}
}