- 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 “”.
Example
Solution 1
Time Complexity: O(S), where S is the sum of all characters in the string array. Space Complexity: O(1)
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
if(strs.length == 1)
return strs[0];
String common = strs[0];
for(int i = 1; i < strs.length; i++) {
String temp = "";
for(int j = 0; j < common.length() && j < strs[i].length(); j++) {
if(common.charAt(j) == strs[i].charAt(j))
temp+=common.charAt(j);
else
break;
}
common = temp;
}
return common;
}
}
==Solution 2: horizontal search ==
Time Complexity: O(S), where S is the sum of all characters in all strings. Space Complexity: O(1)
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
String prefix = strs[0];
for(int i = 1; i < strs.length; i++) {
while(strs[i].indexOf(prefix)!= 0) {
prefix = prefix.substring(0, prefix.length()-1);
if(prefix == "")
return prefix;
}
}
return prefix;
}
}
Solution 3: vertical search
Time Complexity: O(S), where S is the sum of all characters in all strings. In the worst case, there are n same string having m characters. So S = mn. In the best case, there are nminLen.
Space Complexity: O(1)
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
for(int i = 0; i < strs[0].length(); i++) {
char c = strs[0].charAt(i);
for(int j = 1; j < strs.length; j++) {
//check if the length of strs[j] is smaller than the prefix
if(strs[j].length() == i || strs[j].charAt(i) != c)
return strs[0].substring(0,i);
}
}
return strs[0];
}
}
Solution 4: Divide and conquer
Time Complexity: O(S), where S is the sum of all characters in all strings.
Space Complexity: O(m⋅log(n)). As the length of the string array is n, we need to do log(n) recursions. Each iteration need m space to store the result.
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0)
return "";
return longestCommonPrefix(strs, 0, strs.length-1);
}
public String longestCommonPrefix(String[] strs, int left, int right) {
if(left == right)
return strs[left];
else {
int mid = (left+right)/2;
String lcpLeft = longestCommonPrefix(strs, left, mid);
String lcpRight = longestCommonPrefix(strs, mid+1, right);
return commonPrefix(lcpLeft, lcpRight);
}
}
public String commonPrefix(String l, String r) {
int min = Math.min(l.length(), r.length());
for(int i = 0; i < min; i++) {
if(l.charAt(i) != r.charAt(i))
return l.substring(0, i);
}
return l.substring(0, min);
}
}
Solution 5: Binary search
==Time Complexity: O(S *log(n)), where S is the sum of all character of all strings. Since the binary search will go through log(n) iterations and for each iteration there are S=m⋅n comparisons,
Space Complexity: O(1)
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0)
return "";
int min = strs[0].length();
for(int i = 1; i < strs.length; i++)
min = Math.min(min, strs[i].length());
int low = 1;
int high = min;
while(low <= high) {
int middle = (low+high)/2;
if(isCommonPrefix(strs, middle))
low = middle + 1;
else
high = middle - 1;
}
return strs[0].substring(0, (low+high)/2);
}
private boolean isCommonPrefix(String[] strs, int len) {
String temp = strs[0].substring(0, len);
for(int i = 1; i < strs.length; i++) {
if(!strs[i].startsWith(temp))
return false;
}
return true;
}
}