Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 042029d

Browse files
solves #68: Text Justification in java
1 parent 4030185 commit 042029d

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
| 64 | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum) | [![Java](assets/java.png)](src/MinimumPathSum.java) [![Python](assets/python.png)](python/minimum_path_sum.py) | |
6969
| 66 | [Plus One](https://leetcode.com/problems/plus-one) | [![Java](assets/java.png)](src/PlusOne.java) [![Python](assets/python.png)](python/plus_one.py) | |
7070
| 67 | [Add Binary](https://leetcode.com/problems/add-binary) | [![Java](assets/java.png)](src/AddBinary.java) [![Python](assets/python.png)](python/add_binary.py) | |
71+
| 68 | [Text Justification](https://leetcode.com/problems/text-justification) | [![Java](assets/java.png)](src/AddBinary.java) | |
7172
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx) | [![Java](assets/java.png)](src/Sqrtx.java) [![Python](assets/python.png)](python/sqrt.py) | |
7273
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [![Java](assets/java.png)](src/ClimbingStairs.java) [![Python](assets/python.png)](python/climbing_stairs.py) | |
7374
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path) | [![Java](assets/java.png)](src/SimplifyPath.java) | |

src/HelloWorld.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
public class HelloWorld {
2-
public int lengthOfLastWord(String s) {
3-
int i = s.length() - 1;
2+
public int strStr(String haystack, String needle) {
3+
if (needle.length() > haystack.length()) {
4+
return -1;
5+
}
46

5-
// skip all spaces at the end of the word
6-
while (i >= 0 && s.charAt(i) == ' ') {
7-
i--;
7+
for (int i = 0 ; i < haystack.length() ; i++) {
8+
if (needle.charAt(0) == haystack.charAt(i) && containsAt(haystack, needle, i)) {
9+
return i;
10+
}
811
}
912

10-
final int wordEndIndex = i;
13+
return -1;
14+
}
1115

12-
// arrive at the start of the word
13-
while (i >= 0 && s.charAt(i) != ' ') {
14-
i--;
16+
private static boolean containsAt(String haystack, String needle, int i) {
17+
if (needle.length() > haystack.length() - i) {
18+
return false;
1519
}
16-
17-
return wordEndIndex - i;
20+
for (int index = i + 1 ; index < haystack.length() && index - i < needle.length() ; index++) {
21+
if (needle.charAt(index - i) != haystack.charAt(index)) {
22+
return false;
23+
}
24+
}
25+
return true;
1826
}
1927
}

src/LongestCommonPrefix.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// https://leetcode.com/problems/longest-common-prefix/
1+
// https://leetcode.com/problems/longest-common-prefix
2+
// T: O(|array| * |array[i]|)
3+
// S: O(|array[i]|)
24

35
public class LongestCommonPrefix {
46
public String longestCommonPrefix(String[] array) {

src/TextJustification.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// https://leetcode.com/problems/text-justification
2+
// T: O(|words|)
3+
// S: O(|words|)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class TextJustification {
9+
public List<String> fullJustify(String[] words, int maxWidth) {
10+
List<String> ans = new ArrayList<>();
11+
int i = 0;
12+
13+
while (i < words.length) {
14+
List<String> currentLine = getWords(i, words, maxWidth);
15+
i += currentLine.size();
16+
ans.add(createLine(currentLine, i, words, maxWidth));
17+
}
18+
19+
return ans;
20+
}
21+
22+
private List<String> getWords(int i, String[] words, int maxWidth) {
23+
List<String> currentLine = new ArrayList<>();
24+
int currLength = 0;
25+
26+
while (i < words.length && currLength + words[i].length() <= maxWidth) {
27+
currentLine.add(words[i]);
28+
currLength += words[i].length() + 1;
29+
i++;
30+
}
31+
32+
return currentLine;
33+
}
34+
35+
private String createLine(
36+
List<String> line,
37+
int i,
38+
String[] words,
39+
int maxWidth
40+
) {
41+
int baseLength = -1;
42+
for (String word : line) {
43+
baseLength += word.length() + 1;
44+
}
45+
46+
int extraSpaces = maxWidth - baseLength;
47+
48+
if (line.size() == 1 || i == words.length) {
49+
return String.join(" ", line) + " ".repeat(extraSpaces);
50+
}
51+
52+
int wordCount = line.size() - 1;
53+
int spacesPerWord = extraSpaces / wordCount;
54+
int needsExtraSpace = extraSpaces % wordCount;
55+
56+
for (int j = 0; j < needsExtraSpace; j++) {
57+
line.set(j, line.get(j) + " ");
58+
}
59+
60+
for (int j = 0; j < wordCount; j++) {
61+
line.set(j, line.get(j) + " ".repeat(spacesPerWord));
62+
}
63+
64+
return String.join(" ", line);
65+
}
66+
}

0 commit comments

Comments
 (0)