-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainClass.java
55 lines (44 loc) · 1.58 KB
/
MainClass.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
class Solution {
public String longestCommonPrefix(String[] strs) {
char p;
int prefixLength = 0;
boolean stop = false;
if (strs.length == 0) return "";
if (strs.length == 1) return strs[0];
for (int j=0; stop==false && j<strs[0].length(); j++) {
p = strs[0].charAt(j);
for (int i=1; i<strs.length; i++) {
if (j >= strs[i].length()) {
stop = true;
break;
}
if ( p != strs[i].charAt(j) ){
stop = true;
break;
}
}
if (stop == false) prefixLength += 1;
}
return strs[0].substring(0, prefixLength);
}
}
public class MainClass {
public static String[] stringToStringArray(String line) {
JsonArray jsonArray = JsonArray.readFrom(line);
String[] arr = new String[jsonArray.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = jsonArray.get(i).asString();
}
return arr;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
String[] strs = stringToStringArray(line);
String ret = new Solution().longestCommonPrefix(strs);
String out = (ret);
System.out.print(out);
}
}
}