Longest Common Prefix
/**
* Write a function to find the longest common prefix string amongst an array of strings.
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
int n = strs.length;
for(int i = 0; i < strs[0].length(); i++){
for(int j = 1; j < n; j++){
// pay attention to the sequence, or it will throw out the runtime error.
// because the charAt(i) function could not be checked before the length of the string j is guaranteed.
if(strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)){
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}