tags:

views:

195

answers:

7
int n = string.numDifferences("noob", "newb"); // 2

??

+2  A: 

http://en.wikipedia.org/wiki/Levenshtein_distance

??

polygenelubricants
????????????????????????????????
Pirate for Profit
http://en.wikipedia.org/wiki/Hamming_distance ??
polygenelubricants
@polygenelubricants - my first though was Levenshtein, but then since the example is of two `equal` length strings a simpler hamming distance would suffice as you say.
Binary Nerd
+5  A: 

Take a look at one of the methods for measuring edit distance.

Binary Nerd
A: 
import math
def differences(s1, s2):
    count = 0
    for i in range(len(s1)):
        count += int(s1[i] != s2[1])
#    count += math.sqrt( (len(s1) - len(s2)) **2) #add this line if the two strings are of different length and differences counts the how many characters one string has more than the other.
    return count

Hope this helps

inspectorG4dget
Why not `math.fabs`?
Konstantin Spirin
A: 

Assuming that you only want to compare characters at the same indices, the following C# solution (using methods provided by LINQ) should do the trick:

var count = s1.Zip(s2, (c1, c2) => c1 == c2 ? 0 : 1).Sum();

This "zips" the two strings, and then returns 0 for each index where the characters are the same and 1 for each index where they differ. Then we simply sum the numbers and we get the result.

Tomas Petricek
See my comment for @Anthony's solution above. It's the same problem here: you assume naive transformation from `s1` to `s2`, and that doesn't yield the shortest edit distance.
wilhelmtell
@wilhelmtell: Yes, and I mention that in the first sentence :-). It's not clear whether the question is how to calculate simple naive count like this or something more complicated...
Tomas Petricek
A: 

I would perhaps do this as an extension method (C# code)

static class StringExtensions
{
    public static int NumberOfDifferences(this string input, string other)
    {
        // difference in lengths is initial difference value
        int difference = Math.Abs(input.Length - other.Length);

        for (int i = 0; i < Math.Min(input.Length, other.Length); i++)
        {
            if (input[i] != other[i])
                difference++;
        }

        return difference;
    }
}

You could call it like

"blah".NumberOfDifferences("foo");
Anthony Pegram
This algorithm is incorrect. Proof:`"helo".NumberOfDifferences("hello")` returns `2`, when it should return `1` (simply insert an `l` after the `e` or before the `o`).
wilhelmtell
The other way of looking at it is that it is different and character indices 3 and 4, hence the differences being 2. But you introduce a good point as to how differences should be classified. It goes back to Charles Duffy's comment to the question, what constitutes a difference
Anthony Pegram
+8  A: 

The number you are trying to find is called the edit distance. Wikipedia lists several algorithms you might want to use; the Hamming distance is a very common way of finding the edit difference between two strings of the same length (it's often used in error-correcting codes); the Levenshtein distance is similar, but also takes insertions and deletions into account. Wikipedia, of course, lists several others (e.g. Damerau-Levenshtein distance, which includes transpositions); I don't know which you want, as I'm no expert and the choice is domain-specific. One of these, though, should do the trick.

Antal S-Z
+1  A: 

You already got excellent answers if you mean "edit distance". If you just mean "number of characters that differ" (for two strings of the same length), in Python, the simplest approach would be:

sum(c1!=c2 for c1, c2 in zip(s1, s2))

and if you also want to add the length difference, append

+ abs(len(s1) - len(s2))

Of course, if you do want edit distances, this approach would be far too simplistic;-).

Alex Martelli