You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
578 B

package class28;
public class Problem_0014_LongestCommonPrefix {
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
char[] chs = strs[0].toCharArray();
int min = Integer.MAX_VALUE;
for (String str : strs) {
char[] tmp = str.toCharArray();
int index = 0;
while (index < tmp.length && index < chs.length) {
if (chs[index] != tmp[index]) {
break;
}
index++;
}
min = Math.min(index, min);
if (min == 0) {
return "";
}
}
return strs[0].substring(0, min);
}
}