-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLonComPre.java
75 lines (74 loc) · 1.68 KB
/
LonComPre.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
74
75
/********************************************************
> File Name:14LonComPre.java
> Auther: ihochang
> Mail: [email protected]
> Created Time: Wed Jan 6 17:15:09 2016
*********************************************************/
public class LonComPre {
public String longestCommonPrefix(String[] strs) {
int strSize = strs.length;
if (strSize == 0) {
return "";
}
/*if (strSize == 1) {
return strs[0];
}*/
int[] strLen = new int[strSize];
for (int i = 0;i<strSize;i++) {
strLen[i] = strs[i].length();
}
int idx = minStr(strLen,strSize);
String minStr = strs[idx];
int aflag = 0;
String temp = "";
String tcomPre = "";
String comPre = "";
int minStrSize = minStr.length();
for (int i = 0;i<minStrSize;i++) {
tcomPre = minStr.substring(0,minStrSize-i);
System.out.println("host "+tcomPre);
for (int j = 0;j<strSize;j++) {
if ((j==idx)&&(j!=strSize-1)) {
System.out.println(j);
continue;
}
temp = strs[j].substring(0,minStrSize-i);
System.out.println(j+" "+temp);
if (!temp.equals(tcomPre)) {
aflag = 1;
break;
}
if (j==strSize-1) {
aflag = 0;
comPre = tcomPre;
}
}
if (aflag == 0) {
break;
}
}
return comPre;
}
public int minStr(int[] a, int strSize) {
int min = 0;
int temp = 0;
int idx = 0;
for (int i = 0;i<strSize;i++) {
if (i == 0) {
temp = a[i];
} else {
if (a[i]<temp) {
temp = a[i];
idx = i;
}
}
}
return idx;
}
public static void main(String[] args) {
String[] test = new String[] {""};
LonComPre so = new LonComPre();
String answer = so.longestCommonPrefix(test);
System.out.println("answer"+answer);
}
}