-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathlongestCommonPrefix.java
More file actions
39 lines (32 loc) · 1.19 KB
/
longestCommonPrefix.java
File metadata and controls
39 lines (32 loc) · 1.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
class Solution {
public String longestCommonPrefix(String[] strs) {
int minlength = 201;
for(int a=0;a <strs.length;a++){
String s = strs[a];
minlength = Math.min(minlength, s.length());
}
// System.out.println(minlength);
String st = "";
int count = 0;
while (count<= minlength){
for (int b=0; b<strs.length; b++){
if(count< minlength){
char tt =strs[0].charAt(count);
if(strs[b].charAt(count) != tt){
st = strs[0].substring(0, count);
System.out.println(st);
return st;
}
}
else if(count == minlength){
System.out.println(count);
System.out.println(minlength);
return st = strs[0].substring(0, count);
}
}
// System.out.println(count);
count++;
}
return st;
}
}