Complete algorythm for hamming distance