Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

25
LINES

< > BotCompany Repo | #1026033 // levenWithSwapsSubstringIC - find damerau-Levenshtein-closest substring in larger string. complexity O(m*n)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (218L/2K).

1  
static int levenWithSwapsSubstringIC(S haystack, S needle) {
2  
  int m = strL(needle), n = strL(haystack);
3  
4  
  // base cases
5  
  if (containsIgnoreCase(haystack, needle)) ret 0;
6  
7  
  int[] vx = new int[n+1], v0 = new int[n+1], v1 = new int[n+1];
8  
  for i to m: { // iterate over needle
9  
    v1[0] = i+1;
10  
    for j to n: { // iterate over haystack
11  
      int cost = neqic(needle.charAt(i), haystack.charAt(j)) ? 1 : 0;
12  
      int d = min3(v1[j]+1,       // insertion
13  
                       v0[j+1]+1,   // deletion
14  
                       v0[j]+cost); // substitution
15  
      if (i > 0 && j > 0 && equalsIgnoreCase(needle.charAt(i), haystack.charAt(j-1)) && equalsIgnoreCase(needle.charAt(i-1), haystack.charAt(j)))
16  
        d = min(d, vx[j-1] + 1); // transposition
17  
      v1[j+1] = d;
18  
    }
19  
    int[] temp = vx;
20  
    vx = v0;
21  
    v0 = v1;
22  
    v1 = temp;
23  
  }
24  
  ret minOfIntArray(v0);
25  
}

Author comment

Began life as a copy of #1023862

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1026033
Snippet name: levenWithSwapsSubstringIC - find damerau-Levenshtein-closest substring in larger string. complexity O(m*n)
Eternal ID of this version: #1026033/3
Text MD5: c16bea816de0b9c3051e066e51499887
Transpilation MD5: c8dc8feaefd0e3e167e916754e00b017
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-11 15:05:26
Source code size: 892 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 148 / 224
Version history: 2 change(s)
Referenced in: [show references]