JavaScript implements the method for finding the largest public substring, while javascript implements the largest

Source: Internet
Author: User

JavaScript implements the method for finding the largest public substring, while javascript implements the largest

This example describes how to calculate the maximum public substring in JavaScript. We will share this with you for your reference. The details are as follows:

To obtain the largest public substring, a common practice is to use a matrix. Assume that there are strings abcdefg and abcd, the matrix shown in the following table can be formed.

A B C D E F G
A 1 0 0 0 0 0 0
B 0 1 0 0 0 0 0
C 0 0 1 0 0 0 0
D 0 0 0 1 0 0 0

Compare each of the two strings. If the two strings match, the value is 1, and if the two strings do not match, the value is 0. Then, find the sequence with the longest diagonal line of 1, that is, the largest public substring. Looking at the preceding separation, it seems that we have to use a two-dimensional array. It is not very cost-effective when both strings are large. can we further optimize it?

Yes, you need to change the policy, if the match, then the value of the item is set to 1, but its diagonal a [I-1, J-1] (I> 1 & j> 1) in this way, only one-dimensional array can be used.

Use one string as the "row" and the other as the "column" to compare the values of two strings. Use another variable to record the maximum value of the array and the starting position of the string. The Code is as follows:

Function LCS (str1, str2) {if (str1 = "" | str2 = "") {return "";} var len1 = str1.length; var len2 = str2.length; var a = new Array (len1); var maxLen = 0; var maxPos = 0; for (var I = 0; I <len1; I ++) {// row for (var j = len2-1; j> = 0; j --) {// column if (str1.charAt (j) = str2.charAt (I )) {if (I = 0 | j = 0) {a [j] = 1 ;} else {a [j] = a [j-1] + 1 ;}} else {a [j] = 0 ;}if (a [j]> maxLen) {maxLen = a [j]; maxPos = j ;}} return str1.substr (maxPos-maxLen + 1, maxLen );}

But the code is not optimal. Why? Because the above statement must be completed after both layers of loops. Is there any faster way?

The string a and string B have the length of len1 and len2 respectively. The public character substring must be <= Math. min (len1, len2), and the Sub string must be continuous, and must be a, B Sub string.

function findMaxSubStr(s1,s2){ var str= "",  L1=s1.length,  L2=s2.length; if (L1>L2){  var s3=s1;  s1=s2;  s2=s3;  s3 = null;  L1=s2.length;  L2 = s1.length; } for (var i=L1; i > 0; i--) {  for (var j= 0; j <= L2 - i && j < L1; j++){   str = s1.substr(j, i);   if (s2.indexOf(str) >= 0) {    return str;   }  } } return "";}

First, compare the lengths of s1 and s2, and then take the shorter string.substr(idex, len)So take the shorter string to get its child string, and then judge whether it exists in the longer string. If it is saved, return directly; otherwise, remove another character.

Complete example:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.