Skip to content

Commit f215381

Browse files
committed
Runtime: 4 ms (Top 15.97%) | Memory: 42.1 MB (Top 76.75%)
1 parent e289aab commit f215381

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

scripts/algorithms/T/Text Justification/Text Justification.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
// Runtime: 4 ms (Top 15.97%) | Memory: 42.1 MB (Top 76.75%)
12
class Solution {
23
public List<String> fullJustify(String[] words, int maxWidth) {
34
List<String> unBalanced = new ArrayList<>();
45
List<String> balanced = new ArrayList<>();
56
int numSpaces = 0;
6-
7+
78
StringBuffer sb = new StringBuffer();
8-
//Following code creates a list of unbalanced lines by appending words and 1 space between them
9+
//Following code creates a list of unbalanced lines by appending words and 1 space between them
910
for(String word : words){
10-
11+
1112
if(sb.length() == 0){
1213
sb.append(word);
1314
}else{
@@ -18,18 +19,18 @@ public List<String> fullJustify(String[] words, int maxWidth) {
1819
sb = new StringBuffer(word);
1920
}
2021
}
21-
22+
2223
}
23-
24+
2425
if(sb.length() >0){
2526
unBalanced.add(sb.toString());
2627
}
27-
28+
2829
for(int j = 0; j < unBalanced.size(); j++){
2930
String line = unBalanced.get(j);
3031
numSpaces = maxWidth - line.length();
3132
StringBuffer lineB = new StringBuffer(line);
32-
//This if block handles either last line or the scenario where in there's only one word in any sentence and hence no spaces
33+
//This if block handles either last line or the scenario where in there's only one word in any sentence and hence no spaces
3334
if(j == unBalanced.size()-1 || !line.contains(" ")){
3435
int tempSpaces = maxWidth - lineB.length();
3536
while(tempSpaces > 0){
@@ -40,7 +41,7 @@ public List<String> fullJustify(String[] words, int maxWidth) {
4041
continue;
4142
};
4243
// The following block checks for each character and appends 1 space during each loop
43-
//If there are still spaces left at the end of the String, again start from beggining and append spaces after each word
44+
//If there are still spaces left at the end of the String, again start from beggining and append spaces after each word
4445
while(numSpaces > 0){
4546
int i = 0;
4647
while(i < lineB.length() - 1){
@@ -55,7 +56,7 @@ public List<String> fullJustify(String[] words, int maxWidth) {
5556
}
5657
balanced.add(lineB.toString());
5758
}
58-
59+
5960
return balanced;
6061
}
61-
}
62+
}

0 commit comments

Comments
 (0)