Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. private static int distLowenstein(String S1, String S2) {
  2. int m = S1.length(), n = S2.length();
  3. int[] D1;
  4. int[] D2 = new int[n + 1];
  5.  
  6. for (int i = 0; i <= n; i++)
  7. D2[i] = i;
  8.  
  9. for (int i = 1; i <= m; i++) {
  10. D1 = D2;
  11. D2 = new int[n + 1];
  12. for (int j = 0; j <= n; j++) {
  13. if (j == 0) D2[j] = i;
  14. else {
  15. int cost = (S1.charAt(i - 1) != S2.charAt(j - 1)) ? 1 : 0;
  16. if (D2[j - 1] < D1[j] && D2[j - 1] < D1[j - 1] + cost)
  17. D2[j] = D2[j - 1] + 1;
  18. else if (D1[j] < D1[j - 1] + cost)
  19. D2[j] = D1[j] + 1;
  20. else
  21. D2[j] = D1[j - 1] + cost;
  22. }
  23. }
  24. }
  25. return D2[n];
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement