Longest Common Prefix
Example
public class Solution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public String longestCommonPrefix(String[] strs) {
// write your code here
if(strs == null || strs.length == 0){
return "";
}
String res = "";
for (int i = 0; i < strs[0].length();i++){
char c = strs[0].charAt(i);
for(int j = 1; j < strs.length;j++){
if(i >= strs[j].length() || strs[j].charAt(i) != c){
return res;
}
}
res = res + c;
}
return res;
}
}Last updated