str1.indexOf(str2)
The first position that str2 appears in str1.
str1.indexOf(str2)!=0
str2 is not the prefix of str1 because str2 doesn’t appear in str1[0].
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "";
}
String pre = strs[0];
int i = 1;
while(i < strs.length){
while(strs[i].indexOf(pre) != 0){
pre = pre.substring(0,pre.length()-1);
}
i++;
}
return pre;
}
}