Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
class Solution {
public String longestCommonPrefix(String[] v) {
StringBuilder ans = new StringBuilder();
Arrays.sort(v);
String first = v[0];
String last = v[v.length-1];
for (int i=0; i<Math.min(first.length(), last.length()); i++) {
if (first.charAt(i) != last.charAt(i)) {
return ans.toString();
}
ans.append(first.charAt(i));
}
return ans.toString();
}
}
Posted by Jamie Meyer 25 days ago