forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
74 lines (60 loc) · 1.54 KB
/
LongestCommonPrefix.java
File metadata and controls
74 lines (60 loc) · 1.54 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
//brute force solution. could not pass the larger judger.
public class Solution {
public String longestCommonPrefix(String[] strs) {
// Start typing your Java solution below
// DO NOT write main() function
int len = strs.length;
if(len==0) return "";
if(len==1) return strs[0];
String temp = strs[0];
String re = "";
for(int i=0; i<temp.length(); i++){
char ctemp = temp.charAt(i);
for(int j=1; j<len; j++){
if(strs[j].length()==i || strs[j].charAt(i) != ctemp)
return re;
}
re += ctemp;
}
return re;
}
}
//compair pairly, passed both test
public class Solution {
public String longestCommonPrefix(String[] strs) {
// Start typing your Java solution below
// DO NOT write main() function
int len = strs.length;
if(len==0) return "";
if(len==1) return strs[0];
String[] temp = strs;
int end = len-1;
while(end>0){
end = pairCompare(temp,0,end);
}
return temp[0];
}
public int pairCompare(String[] strs, int start, int end){
int i=start;
int j=end;
while(j>i){
strs[i]= prefixOftwo(strs[i],strs[j]);
i++;
j--;
}
return j;
}
public String prefixOftwo(String s1, String s2){
String re = "";
int i =0;
while(i<s1.length() && i<s2.length()){
if(s1.charAt(i) == s2.charAt(i)){
re += s1.charAt(i);
i++;
}
else
break;
}
return re;
}
}