Levenshtein's JavaScript algorithm implements a fast and efficient levenshtein Algorithm Implementation

Source: Internet
Author: User
Levenshtein Algorithm

For the levenshtein algorithm definition and algorithm principles, see this article.Article: A fast and efficient levenshtein Algorithm Implementation

Javascript implementation
 Function  Levenshteindistance (S, T ){  If (S. length> T. Length ){  VaR Temp = S; s = T; t = Temp;  Delete  Temp ;}  VaR N =S. length;  VaR M = T. length;  If (M = 0 ){  Return  N ;}  Else   If (N = 0 ){  Return  M ;}  VaR V0 = [];  For (VaR I = 0; I <= m; I ++ ) {V0 [I] = I ;}  VaR V1 = New Array (n + 1 );  VaR Cost = 0 ;  For ( VaR I = 1; I <= N; I ++ ){  If (I> 1 ) {V0 = V1.slice (0);} V1 [ 0] = I;  For ( VaR J = 1; j <= m; j ++ ){  If (S [I-1]. tolowercase () = T [J-1 ]. Tolowercase () {cost = 0 ;}  Else  {Cost = 1 ;} V1 [J] = Math. Min. Call ( Null , V1 [J-1] + 1, V0 [J] + 1, V0 [J-1] +Cost );}}  Return  V1.pop ();}  //  Test Levenshteindistance ("Gumbo", "gambol "); //  Output 2 
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.