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

37
LINES

< > BotCompany Repo | #1024144 // levenWithSwapsIC - case-insensitive Damerau-Levenshtein string distance function (adjacent char transpositions have cost 1)

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

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

static int levenWithSwapsIC(S s, S t) {
  // degenerate cases
  if (s.equals(t)) return 0;
  int ls = s.length(), lt = t.length();
  if (ls == 0) return lt;
  if (lt == 0) return ls;

  int[] vx = new int[lt + 1];
  int[] v0 = new int[lt + 1];
  int[] v1 = new int[lt + 1];

  for (int i = 0; i < v0.length; i++) v0[i] = i;

  for (int i = 0; i < ls; i++) {
      v1[0] = i + 1;

      for (int j = 0; j < lt; j++) {
          int cost = equalsIgnoreCase(s.charAt(i), t.charAt(j)) ? 0 : 1;
          int d = min3(
            v1[j] + 1,     // delete
            v0[j + 1] + 1, // insert
            v0[j] + cost); // edit
            
          if (i > 0 && j > 0 && equalsIgnoreCase(s.charAt(i), t.charAt(j-1)) && equalsIgnoreCase(s.charAt(i-1), t.charAt(j)))
            d = min(d, vx[j-1] + 1); // transposition
          v1[j + 1] = d;
      }

      // rotate arrays
      int[] v = vx;
      vx = v0;
      v0 = v1;
      v1 = v;
  }

  return v0[lt];
}

Author comment

Began life as a copy of #1022301

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1024144
Snippet name: levenWithSwapsIC - case-insensitive Damerau-Levenshtein string distance function (adjacent char transpositions have cost 1)
Eternal ID of this version: #1024144/6
Text MD5: 4777580fcf893e6284c8f78f9d0bf743
Transpilation MD5: 86544ef93c0f2916d409f8ba6099e65c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-11 14:58:35
Source code size: 996 bytes / 37 lines
Pitched / IR pitched: No / No
Views / Downloads: 272 / 353
Version history: 5 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)