-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path14-longest-common-prefix.java
More file actions
101 lines (83 loc) · 4.19 KB
/
14-longest-common-prefix.java
File metadata and controls
101 lines (83 loc) · 4.19 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Arrays;
class Solution {
// Approach 1: Horizontal scanning
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(prefix) != 0) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
return "";
}
}
}
return prefix;
}
// Approach 2: Vertical scanning
public String longestCommonPrefixVertical(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
for (int i = 0; i < strs[0].length(); i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
// Approach 3: Divide and Conquer
public String longestCommonPrefixDivideConquer(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
return longestCommonPrefixRecursive(strs, 0, strs.length - 1);
}
private String longestCommonPrefixRecursive(String[] strs, int left, int right) {
if (left == right) {
return strs[left];
}
int mid = (left + right) / 2;
String lcpLeft = longestCommonPrefixRecursive(strs, left, mid);
String lcpRight = longestCommonPrefixRecursive(strs, mid + 1, right);
return commonPrefix(lcpLeft, lcpRight);
}
private String commonPrefix(String str1, String str2) {
int minLength = Math.min(str1.length(), str2.length());
for (int i = 0; i < minLength; i++) {
if (str1.charAt(i) != str2.charAt(i)) {
return str1.substring(0, i);
}
}
return str1.substring(0, minLength);
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println("Testing Longest Common Prefix (Horizontal Scanning):");
String[] strs1 = {"flower", "flow", "flight"};
System.out.println("Input: [\"flower\",\"flow\",\"flight\"] -> Output: \"" + sol.longestCommonPrefix(strs1) + "\" (Expected: \"fl\")");
String[] strs2 = {"dog", "racecar", "car"};
System.out.println("Input: [\"dog\",\"racecar\",\"car\"] -> Output: \"" + sol.longestCommonPrefix(strs2) + "\" (Expected: \"\")");
String[] strs3 = {"apple", "apricot", "april"};
System.out.println("Input: [\"apple\",\"apricot\",\"april\"] -> Output: \"" + sol.longestCommonPrefix(strs3) + "\" (Expected: \"ap\")");
System.out.println("\nTesting Longest Common Prefix (Vertical Scanning):\n");
String[] strs4 = {"flower", "flow", "flight"};
System.out.println("Input: [\"flower\",\"flow\",\"flight\"] -> Output: \"" + sol.longestCommonPrefixVertical(strs4) + "\" (Expected: \"fl\")");
String[] strs5 = {"dog", "racecar", "car"};
System.out.println("Input: [\"dog\",\"racecar\",\"car\"] -> Output: \"" + sol.longestCommonPrefixVertical(strs5) + "\" (Expected: \"\")");
String[] strs6 = {"apple", "apricot", "april"};
System.out.println("Input: [\"apple\",\"apricot\",\"april\"] -> Output: \"" + sol.longestCommonPrefixVertical(strs6) + "\" (Expected: \"ap\")");
System.out.println("\nTesting Longest Common Prefix (Divide and Conquer):\n");
String[] strs7 = {"flower", "flow", "flight"};
System.out.println("Input: [\"flower\",\"flow\",\"flight\"] -> Output: \"" + sol.longestCommonPrefixDivideConquer(strs7) + "\" (Expected: \"fl\")");
String[] strs8 = {"dog", "racecar", "car"};
System.out.println("Input: [\"dog\",\"racecar\",\"car\"] -> Output: \"" + sol.longestCommonPrefixDivideConquer(strs8) + "\" (Expected: \"\")");
String[] strs9 = {"apple", "apricot", "april"};
System.out.println("Input: [\"apple\",\"apricot\",\"april\"] -> Output: \"" + sol.longestCommonPrefixDivideConquer(strs9) + "\" (Expected: \"ap\")");
}
}