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

Commit 8cff083

Browse files
refactor 14
1 parent 49e6059 commit 8cff083

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,25 @@
44
* 14. Longest Common Prefix
55
*
66
* Write a function to find the longest common prefix string amongst an array of strings.
7+
*
8+
* If there is no common prefix, return an empty string "".
9+
*
10+
* Example 1:
11+
* Input: ["flower","flow","flight"]
12+
* Output: "fl"
13+
*
14+
* Example 2:
15+
* Input: ["dog","racecar","car"]
16+
* Output: ""
17+
* Explanation: There is no common prefix among the input strings.
18+
*
19+
* Note:
20+
* All given inputs are in lowercase letters a-z.
721
*/
822

923
public class _14 {
1024

1125
public static class Solution1 {
12-
public String longestCommonPrefix(String[] strs) {
13-
if (strs.length == 0) {
14-
return "";
15-
}
16-
17-
int i = 0;
18-
String prefix = "";
19-
String result;
20-
boolean broken = false;
21-
while (true) {
22-
i++;
23-
result = prefix;
24-
if (i > strs[0].length()) {
25-
break;//this will break out the while loop
26-
}
27-
prefix = strs[0].substring(0, i);
28-
for (String word : strs) {
29-
if (i > word.length() || !word.startsWith(prefix)) {
30-
broken = true;
31-
break;//this will only break out of the for loop
32-
}
33-
}
34-
if (broken) {
35-
break;//this will break out the while loop
36-
}
37-
}
38-
return result;
39-
}
40-
}
41-
42-
public static class Solution2 {
4326
//horizontal scan
4427
public String longestCommonPrefix(String[] strs) {
4528
if (strs.length == 0) {
@@ -58,7 +41,7 @@ public String longestCommonPrefix(String[] strs) {
5841
}
5942
}
6043

61-
public static class Solution3 {
44+
public static class Solution2 {
6245
//vertical scan
6346
public String longestCommonPrefix(String[] strs) {
6447
if (strs.length == 0) {

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,61 @@
99
public class _14Test {
1010
private static _14.Solution1 solution1;
1111
private static _14.Solution2 solution2;
12-
private static _14.Solution3 solution3;
1312
private static String[] strs;
1413

1514
@BeforeClass
1615
public static void setup() {
1716
solution1 = new _14.Solution1();
1817
solution2 = new _14.Solution2();
19-
solution3 = new _14.Solution3();
2018
}
2119

2220
@Test
2321
public void test1() {
2422
strs = new String[]{"a", "b"};
2523
assertEquals("", solution1.longestCommonPrefix(strs));
2624
assertEquals("", solution2.longestCommonPrefix(strs));
27-
assertEquals("", solution3.longestCommonPrefix(strs));
2825
}
2926

3027
@Test
3128
public void test2() {
3229
strs = new String[]{"leetcode", "lead"};
3330
assertEquals("le", solution1.longestCommonPrefix(strs));
3431
assertEquals("le", solution2.longestCommonPrefix(strs));
35-
assertEquals("le", solution3.longestCommonPrefix(strs));
3632
}
3733

3834
@Test
3935
public void test3() {
4036
strs = new String[]{"leetcode", "code"};
4137
assertEquals("", solution1.longestCommonPrefix(strs));
4238
assertEquals("", solution2.longestCommonPrefix(strs));
43-
assertEquals("", solution3.longestCommonPrefix(strs));
39+
}
40+
41+
@Test
42+
public void test4() {
43+
strs = new String[]{"flower", "flow", "flight"};
44+
assertEquals("fl", solution1.longestCommonPrefix(strs));
45+
assertEquals("fl", solution2.longestCommonPrefix(strs));
46+
}
47+
48+
@Test
49+
public void test5() {
50+
strs = new String[]{};
51+
assertEquals("", solution1.longestCommonPrefix(strs));
52+
assertEquals("", solution2.longestCommonPrefix(strs));
53+
}
54+
55+
@Test
56+
public void test6() {
57+
strs = new String[]{"a"};
58+
assertEquals("a", solution1.longestCommonPrefix(strs));
59+
assertEquals("a", solution2.longestCommonPrefix(strs));
60+
}
61+
62+
@Test
63+
public void test7() {
64+
strs = new String[]{"c", "c"};
65+
assertEquals("c", solution1.longestCommonPrefix(strs));
66+
assertEquals("c", solution2.longestCommonPrefix(strs));
4467
}
4568

4669
}

0 commit comments

Comments
 (0)