Libraryless. Click here for Pure Java version (159L/2K).
1 | static int levenWithSwapsIC(S s, S t) { |
2 | // degenerate cases |
3 | if (s.equals(t)) return 0; |
4 | int ls = s.length(), lt = t.length(); |
5 | if (ls == 0) return lt; |
6 | if (lt == 0) return ls; |
7 | |
8 | int[] vx = new int[lt + 1]; |
9 | int[] v0 = new int[lt + 1]; |
10 | int[] v1 = new int[lt + 1]; |
11 | |
12 | for (int i = 0; i < v0.length; i++) v0[i] = i; |
13 | |
14 | for (int i = 0; i < ls; i++) { |
15 | v1[0] = i + 1; |
16 | |
17 | for (int j = 0; j < lt; j++) { |
18 | int cost = equalsIgnoreCase(s.charAt(i), t.charAt(j)) ? 0 : 1; |
19 | int d = min3( |
20 | v1[j] + 1, // delete |
21 | v0[j + 1] + 1, // insert |
22 | v0[j] + cost); // edit |
23 | |
24 | if (i > 0 && j > 0 && equalsIgnoreCase(s.charAt(i), t.charAt(j-1)) && equalsIgnoreCase(s.charAt(i-1), t.charAt(j))) |
25 | d = min(d, vx[j-1] + 1); // transposition |
26 | v1[j + 1] = d; |
27 | } |
28 | |
29 | // rotate arrays |
30 | int[] v = vx; |
31 | vx = v0; |
32 | v0 = v1; |
33 | v1 = v; |
34 | } |
35 | |
36 | return v0[lt]; |
37 | } |
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: | 271 / 353 |
Version history: | 5 change(s) |
Referenced in: | [show references] |