开发者

How to modify this function to return string instead of int

please take a quick look at this function that I have found on the web.

function longestCommonSubstring(string1, string2){
        // init max value
        var longestCommonSubstring = 0;
        // init 2D array with 0
        var table = Array(string1.length);
     开发者_开发技巧   for(a = 0; a <= string1.length; a++){
                table[a] = Array(string2.length);
                for(b = 0; b <= string2.length; b++){
                        table[a][b] = 0;
                }
        }
        // fill table
        for(var i = 0; i < string1.length; i++){
                for(var j = 0; j < string2.length; j++){
                        if(string1[i]==string2[j]){
                                if(table[i][j] == 0){
                                        table[i+1][j+1] = 1;
                                } else {
                                        table[i+1][j+1] = table[i][j] + 1;
                                }
                                if(table[i+1][j+1] > longestCommonSubstring){
                                        longestCommonSubstring = table[i+1][j+1];
                                }
                        } else {
                                table[i+1][j+1] = 0;
                        }
                }
        }
        return longestCommonSubstring;
}

It returns the length of the longest common substring as an int. Now to my question, is it possible to modify this function, so that it returns the actual string instead of just returning the length of the substring, I'm quite new at programming and thought that just modifying this secetion would help if(string1[i]==string2[j]){ push(string1[i]}, but it isn't that easy, because I don't want every single character that is the same in those 2 strings to be added in that array, only those that are exactly the same. Thanks in advance =)


Well for minimal changes to the existing function you could declare a new variable:

var theCommonString = '';

Then in the middle of the function add a line after this existing one:

longestCommonSubstring = table[i+1][j+1];

that says something like:

theCommonString = string1.substr(i + 1 - longestCommonSubstring,
                                 longestCommonSubstring);

(That i + 1 index may be a little off, I haven't bothered working it out carefully.)

Then at the end just return your new variable instead of the existing one.

Note that if there is more than one common sub string of the same length this will return the last one.


You can just store the whole common substring in the table instead of its length:

function longestCommonSubstring(string1, string2){
        // init max value
        var longestCommonSubstring = "";
        // init 2D array with 0
        var table = Array(string1.length);
        for(a = 0; a <= string1.length; a++){
                table[a] = Array(string2.length);
                for(b = 0; b <= string2.length; b++){
                        table[a][b] = 0;
                }
        }
        // fill table
        for(var i = 0; i < string1.length; i++){
                for(var j = 0; j < string2.length; j++){
                        if(string1[i]==string2[j]){
                                if(table[i][j] == 0){
                                        table[i+1][j+1] = string1[i];
                                } else {
                                        table[i+1][j+1] = table[i][j] + string1[i];
                                }
                                if(table[i+1][j+1].length > longestCommonSubstring.length){
                                        longestCommonSubstring = table[i+1][j+1];
                                }
                        } else {
                                table[i+1][j+1] = 0;
                        }
                }
        }
        return longestCommonSubstring;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜