Refactored HammingDistance to use recursion