Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
82 lines (74 loc) · 1.79 KB

File metadata and controls

82 lines (74 loc) · 1.79 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
public ArrayList<String> fullJustify(String[] words, int L) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<String> res = new ArrayList<String>();
if(L==0){
res.add("");
return res;
}
int i = 0;
int count = 0;
int len = 0;
int j=0;
while(i<words.length){
len += (words[i].length()+1);
if(len-1<=L){
i++;
count++;
continue;
}
else{
fillString(words, res, count, i-count, L, 0);
len = 0;
count=0;
}
}
fillString(words, res, count, i-count, L, 1);
return res;
}
public void fillString(String[] words, ArrayList<String> res, int count, int start, int L, int flag){
StringBuilder sb = new StringBuilder(L);
if(count == 1){
sb.append(words[start]);
for(int i=0; i<L-words[start].length(); i++)
sb.append(" ");
res.add(sb.toString());
return;
}
//calculate spaces
int wordlen = 0;
for(int i = start; i<start+count; i++){
wordlen += words[i].length();
}
// Last line
if(flag == 1){
int totalspace = L-wordlen;
for(int i=0; i<count-1; i++){
sb.append(words[start+i]);
sb.append(" ");
totalspace--;
}
sb.append(words[start+count-1]);
for(int i=0; i<totalspace; i++)
sb.append(" ");
res.add(sb.toString());
return;
}
int everyspacelen = (L-wordlen)/(count-1);
int extraspacelen = (L-wordlen)%(count-1);
String spaces = "";
for(int i=0; i<everyspacelen; i++)
spaces += " ";
for(int i=0; i<count-1; i++){
sb.append(words[start+i]);
sb.append(spaces);
if(extraspacelen>0){
sb.append(" ");
extraspacelen--;
}
}
sb.append(words[start+count-1]);
res.add(sb.toString());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.