Skip to content

Instantly share code, notes, and snippets.

@junkdog
Created February 22, 2013 07:56
Show Gist options
  • Save junkdog/5011625 to your computer and use it in GitHub Desktop.
Save junkdog/5011625 to your computer and use it in GitHub Desktop.
public static String findCommonPrefix(List<String> strings)
{
if (strings.size() == 0)
return "";
String prefix = strings.get(0);
for (int i = 1, s = strings.size(); s > i; i++)
{
String str = strings.get(i);
for (int j = 0, l = Math.min(prefix.length(), str.length()); l > j; j++)
{
if (prefix.charAt(j) != str.charAt(j))
{
prefix = prefix.substring(0, j);
break;
}
}
}
return prefix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment