Tính khoảng cách Hamming

Calculate the Hamming difference between two DNA strands.

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^

The Hamming distance between these two DNA strands is 7.

Đoạn code:

<?php

//
// This is only a SKELETON file for the "Hamming" exercise. It's been provided as a
// convenience to get you started writing code faster.
//

function distance($a, $b)
{
    $chars_a = str_split($a);
    $chars_b = str_split($b);

    if (count($chars_a) !== count($chars_b)) {
        throw new InvalidArgumentException('DNA strands must be of equal length.');
    }

    return count(array_diff_assoc($chars_a, $chars_b));
}