Skip to content

Commit 77ad232

Browse files
refactor 14
1 parent 921c27a commit 77ad232

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727
|737|[Sentence Similarity II](https://leetcode.com/problems/sentence-similarity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_737.java) | O(nlogk + k) n is the length of max(words1, words2), k is the length of pairs| O(k) | Medium| Union Find
2828
|735|[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_735.java) | O(n) | O(n) | Medium | Stack
2929
|734|[Sentence Similarity](https://leetcode.com/problems/sentence-similarity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_734.java) | O(n*k) | O(1) | Easy | HashTable
30-
|733|[Flood Fill](https://leetcode.com/problems/flood-fill/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_733.java) | O(m*n) | O(m*n) | Easy | BFS, DFS
30+
|733|[Flood Fill](https://leetcode.com/problem**__**s/flood-fill/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_733.java) | O(m*n) | O(m*n) | Easy | BFS, DFS
3131
|729|[My Calendar I](https://leetcode.com/problems/my-calendar-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_729.java) | O(n) | O(n) | Medium |
3232
|728|[Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_728.java) | O(n*k) k is the average number of digits of each number in the given array| O(1) | Easy |
3333
|727|[Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_727.java) | O(m*n) | O(m*n) | Hard | DP
@@ -653,7 +653,7 @@ Your ideas/fixes/algorithms are more than welcome!
653653
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_17.java)|O(n*4^n)|O(n)|Medium|Backtracking
654654
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_16.java)|O(nlogn)|O(1)|Medium|Two Pointers
655655
|15|[3Sum](https://leetcode.com/problems/3sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_15.java)|O(n^2)|O(1)|Medium|Two Pointers, Binary Search
656-
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_14.java)| O(n*min(wordLength in this array)) | O(1) | Easy
656+
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_14.java)| O(S) (S is the sum of all characters in all strings) | O(1) | Easy
657657
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer)|[Solution](../master/src/main/java/com/fishercoder/solutions/_13.java)| O(1) | O(1) | Easy | Math, String
658658
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)|O(1)|O(1)|Medium| Math, String
659659
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_11.java)|O(n)|O(1)|Medium|

src/main/java/com/fishercoder/solutions/_14.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public String longestCommonPrefix(String[] strs) {
1616

1717
int i = 0;
1818
String prefix = "";
19-
String result = "";
19+
String result;
2020
boolean broken = false;
2121
while (true) {
2222
i++;
@@ -39,4 +39,40 @@ public String longestCommonPrefix(String[] strs) {
3939
}
4040
}
4141

42+
public static class Solution2 {
43+
//horizontal scan
44+
public String longestCommonPrefix(String[] strs) {
45+
if (strs.length == 0) {
46+
return "";
47+
}
48+
String prefix = strs[0];
49+
for (int i = 1; i < strs.length; i++) {
50+
while (strs[i].indexOf(prefix) != 0) {
51+
prefix = prefix.substring(0, prefix.length() - 1);
52+
if (prefix.isEmpty()) {
53+
return "";
54+
}
55+
}
56+
}
57+
return prefix;
58+
}
59+
}
60+
61+
public static class Solution3 {
62+
//vertical scan
63+
public String longestCommonPrefix(String[] strs) {
64+
if (strs.length == 0) {
65+
return "";
66+
}
67+
for (int i = 0; i < strs[0].length(); i++) {
68+
char c = strs[0].charAt(i);
69+
for (int j = 1; j < strs.length; j++) {
70+
if (i == strs[j].length() || strs[j].charAt(i) != c) {
71+
return strs[0].substring(0, i);
72+
}
73+
}
74+
}
75+
return strs[0];
76+
}
77+
}
4278
}

src/test/java/com/fishercoder/_14Test.java

+22
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,39 @@
88

99
public class _14Test {
1010
private static _14.Solution1 solution1;
11+
private static _14.Solution2 solution2;
12+
private static _14.Solution3 solution3;
1113
private static String[] strs;
1214

1315
@BeforeClass
1416
public static void setup() {
1517
solution1 = new _14.Solution1();
18+
solution2 = new _14.Solution2();
19+
solution3 = new _14.Solution3();
1620
}
1721

1822
@Test
1923
public void test1() {
2024
strs = new String[]{"a", "b"};
2125
assertEquals("", solution1.longestCommonPrefix(strs));
26+
assertEquals("", solution2.longestCommonPrefix(strs));
27+
assertEquals("", solution3.longestCommonPrefix(strs));
28+
}
29+
30+
@Test
31+
public void test2() {
32+
strs = new String[]{"leetcode", "lead"};
33+
assertEquals("le", solution1.longestCommonPrefix(strs));
34+
assertEquals("le", solution2.longestCommonPrefix(strs));
35+
assertEquals("le", solution3.longestCommonPrefix(strs));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
strs = new String[]{"leetcode", "code"};
41+
assertEquals("", solution1.longestCommonPrefix(strs));
42+
assertEquals("", solution2.longestCommonPrefix(strs));
43+
assertEquals("", solution3.longestCommonPrefix(strs));
2244
}
2345

2446
}

0 commit comments

Comments
 (0)