Created
February 22, 2013 07:56
-
-
Save junkdog/5011625 to your computer and use it in GitHub Desktop.
Find common prefix for a list of strings. Slightly modified version of http://tianrunhe.wordpress.com/2012/07/29/find-the-longest-common-prefix-in-an-array-of-strings-longest-common-prefix/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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