4

Does anyone know of a simple way to compare two strings together to generate the "amount of difference" between the two? (in a numeric value) I have been crawling google with little luck on this. And after doing some coding it's not as simple as I had thought. Any clues?

random
  • 9,774
  • 10
  • 66
  • 83
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104

4 Answers4

5

Are you talking about the "Edit Distance"? Do a search on "Levenshtein Distance", on SO or Google. I use the version posted on Stephen Toub's blog

Danimal
  • 7,672
  • 8
  • 47
  • 57
2

You're looking for the Levenshtein distance.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

You might want to look into the Levenshtein and Hamming distances. One calculates edit distance (insert, delete, modify) and the other bit flips.

Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
0

You would need to very clearly define "amount of difference". There's a lot of wiggle room in there.

For example, the old C/C++ function strcmp() function compared character by character and returned the difference the first time they didn't match.

On the other hand, the diff program provides a comprehensive list of differences between two files (which, in once sense, are also strings). How would you quantify that?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794