I need to find the longest common substring of n strings and use the result in my project.
Is there any existing implementation/library in java which already does this?
I need to find the longest common substring of n strings and use the result in my project.
Is there any existing implementation/library in java which already does this?
What about concurrent-trees ?
It is a small (~100 KB) library available in Maven Central. The algorithm uses combination of Radix and Suffix Trees. Which is known to have a linear time complexity (wikipedia).
public static String getLongestCommonSubstring(Collection<String> strings) { LCSubstringSolver solver = new LCSubstringSolver(new DefaultCharSequenceNodeFactory()); for (String s: strings) { solver.add(s); } return solver.getLongestCommonSubstring().toString(); } We can use below code to identify longest common sub string of n Strings
public static String identifyCommonSubStrOfNStr(String [] strArr){ String commonStr=""; String smallStr =""; //identify smallest String for (String s :strArr) { if(smallStr.length()< s.length()){ smallStr=s; } } String tempCom=""; char [] smallStrChars=smallStr.toCharArray(); for (char c: smallStrChars){ tempCom+= c; for (String s :strArr){ if(!s.contains(tempCom)){ tempCom=c; for (String s :strAarr){ if(!s.contains(tempCom)){ tempCom=""; break; } } break; } } if(tempCom!="" && tempCom.length()>commonStr.length()){ commonStr=tempCom; } } return commonStr; } This page pretty much gives exactly what you want in quite a few languages.
public static int longestSubstr(String first, String second) { if (first == null || second == null || first.length() == 0 || second.length() == 0) { return 0; } int maxLen = 0; int fl = first.length(); int sl = second.length(); int[][] table = new int[fl][sl]; for (int i = 0; i < fl; i++) { for (int j = 0; j < sl; j++) { if (first.charAt(i) == second.charAt(j)) { if (i == 0 || j == 0) { table[i][j] = 1; } else { table[i][j] = table[i - 1][j - 1] + 1; } if (table[i][j] > maxLen) { maxLen = table[i][j]; } } } } return maxLen; } Well you could try to extend the version of wikipedia (http://en.wikipedia.org/wiki/Longest_common_substring_problem) for n Strings by putting it into a loop which iterates over all your Strings.
public String findLongestCommonSubstring(String source, String dest) { int table[][] = new int[source.length() + 1][dest.length() + 1]; for (int col = 0; col < table[0].length; col++) { table[0][col] = 0; } for (int row = 0; row < table.length; row++) { table[row][0] = 0; } int max = 0; int index = 0; for (int row = 1; row < table.length; row++) { char sourceChar = source.charAt(row - 1); for (int col = 1; col < table[row].length; col++) { char destChar = dest.charAt(col - 1); if (sourceChar == destChar) { table[row][col] = table[row - 1][col - 1] + 1; if (table[row][col] > max) { max = table[row][col]; index = row; } } else { table[row][col] = 0; } } } return source.substring(index - max, index); }