Skip to content

Instantly share code, notes, and snippets.

@stavrossk
Forked from junkdog/gist:5011625
Last active December 14, 2015 02:09
Show Gist options
  • Save stavrossk/5011657 to your computer and use it in GitHub Desktop.
Save stavrossk/5011657 to your computer and use it in GitHub Desktop.
Find common prefix from a list of strings.
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