Thinking process - I
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
if (strs.length == 1) return strs[0];
int endIndex = 0;
int strIndex = 1;
while (endIndex < strs[strIndex].length()) {
if (endIndex >= strs[strIndex - 1].length() || strs[strIndex].charAt(endIndex) != strs[strIndex - 1].charAt(endIndex)) break;
strIndex++;
if (strIndex == strs.length) {
strIndex = 1;
endIndex++;
}
}
return strs[0].substring(0, endIndex);
}
}
Thinking process - II
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String prev = strs[0];
int strIndex = 1;
while (strIndex < strs.length) {
while (strs[strIndex].indexOf(prev) != 0) {//not common prefix
prev = prev.substring(0, prev.length() - 1);
}
strIndex++;
}
return prev;
}
}