Monday, March 11, 2013

Longest Common Prefix

Find the longest common prefix string amongst an array of strings.

We start with the first string as the longest common prefix. We then go through every string in the array and compare the prefix with them. Update (shorten) the prefix if needed.

public class LongestCommonPrefix {
public static String findLongestCommonPrefix(String[] array) {
String prefix;
if (array.length > 0) {
prefix = array[0];
} else {
return "";
}

for (int i = 1; i < array.length; i++) {
String s = array[i];
int j = 0;
for (; j < Math.min(prefix.length(), s.length()); j++) {
if (prefix.charAt(j) != s.charAt(j))
break;
}

prefix = prefix.substring(0, j);
}

return prefix;
}

public static void main(String args[]) {
String str[] = { "hello", "hellohel", "hellohello" };
System.out.println("Longest common prefix is"
+ findLongestCommonPrefix(str));
}
}

No comments: