参考 : http://www.cnblogs.com/ymind/archive/2012/03/27/fast-memory-efficient-Levenshtein-algorithm.html
  1. function _levenshtein($src, $dst){
  2. if (empty($src)) {
  3. return $dst;
  4. }
  5. if (empty($dst)) {
  6. return $src;
  7. }
  8. $temp = array();
  9. for($i = 0; $i $temp[$i][0] = $i;
  10. }
  11. for($j = 0; $j $temp[0][$j] = $j;
  12. }
  13. for ($i = 1;$i
  14. $src_i = $src{$i - 1};
  15. for ($j = 1; $j
  16. $dst_j = $dst{$j - 1};
  17. if ($src_i == $dst_j) {
  18. $cost = 0;
  19. } else {
  20. $cost = 1;
  21. }
  22. $temp[$i][$j] = min($temp[$i-1][$j]+1, $temp[$i][$j-1]+1, $temp[$i-1][$j-1] + $cost);
  23. }
  24. }
  25. return $temp[$i-1][$j-1];
  26. }
  27. echo _levenshtein("hello", "HElloo");
复制代码


09-19 07:56