2. 14_最长公共前缀
/*
输入: ["flower","flow","flight"]
输出: "fl"
*/
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
if(strs.length == 1) return strs[0];
int k = 0;
StringBuilder sb = new StringBuilder("");
while(k < strs[0].length()){
char c = strs[0].charAt(k);
int i = 1;
while(i < strs.length && k < strs[i].length()){
if(strs[i].charAt(k) == c) i++;
else return sb.toString();
}
if(i < strs.length) return sb.toString();
sb.append(c);
k++;
}
return sb.toString();
}
}