java - longest substring with exclude list of strings -
i using this algorithm find common substring between 2 strings. please, me using array
of common substrings of strings, should ignore in function.
my code in java:
public static string longestsubstring(string str1, string str2) { stringbuilder sb = new stringbuilder(); if (str1 == null || str1.isempty() || str2 == null || str2.isempty()) { return ""; } // java initializes them 0 int[][] num = new int[str1.length()][str2.length()]; int maxlen = 0; int lastsubsbegin = 0; (int = 0; < str1.length(); i++) { (int j = 0; j < str2.length(); j++) { if (str1.charat(i) == str2.charat(j)) { if ((i == 0) || (j == 0)) { num[i][j] = 1; } else { num[i][j] = 1 + num[i - 1][j - 1]; } if (num[i][j] > maxlen) { maxlen = num[i][j]; // generate substring str1 => int thissubsbegin = - num[i][j] + 1; if (lastsubsbegin == thissubsbegin) { //if current lcs same last time block ran sb.append(str1.charat(i)); } else { //this block resets string builder if different lcs found lastsubsbegin = thissubsbegin; sb = new stringbuilder(); sb.append(str1.substring(lastsubsbegin, + 1)); } } } } } return sb.tostring(); }
so, function should looks like:
public static string longestsubstring(string str1, string str2, string[] ignore)
create suffix tree of 1 of strings , run through second see substring can found in suffix tree.
info on suffixtrees: http://en.wikipedia.org/wiki/suffixtree
Comments
Post a Comment