-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestCommonPrefix.java
73 lines (66 loc) · 1.68 KB
/
LongestCommonPrefix.java
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
package ProjectFiles;
/**
* This problem is different than the one leetcode was expecting.
* It gets the LCP for any two strings whereas leetcode is expecting to return the LCP
* for all the strings.
* @author ma64
*
*/
public class LongestCommonPrefix {
public static void main(String[] args) {
String[] st = {"this is a test", "this is also a test"," this is note"};
System.out.println(longestCommonPrefix(st));
String[] st2 = {"c","c", "gds"};
System.out.println(longestCommonPrefix(st2));
String[] st3 = {"ca","caf", "cafd"};
System.out.println(longestCommonPrefix(st3));
String[] st4 = {"","cdad", "cda"};
System.out.println(longestCommonPrefix(st4));
}
public static String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
if(strs.length == 1) return strs[0];
Trie1 t = new Trie1(' ');
t.children = new Trie1[256];
String result = "";
for(String s : strs){
String st = addandSearch(s,t);
if(st.length() > result.length()){
result = st;
}
}
return result;
}
public static String addandSearch(String st, Trie1 t){
String output = "";
for(int i = 0; i < st.length(); i++){
char c = st.charAt(i);
if(t.children == null){
t.children = new Trie1[256];
}
if(t.children[c] != null){
t = t.children[c];
output += c;
}
else{
t.children[c] = new Trie1(c);
t = t.children[c];
t.children = new Trie1[256];
}
}
return output;
}
public static String longestCommonPrefixAmongAllStrings(String[] strs) {
String result = "";
for(String s : strs){
String lcp = longestCommonPrefix();
}
}
}
class Trie1{
Trie1[] children;
char value;
Trie1(char c){
value = c;
}
}