Algorithm 101: 3 Ways to Find Hamming Distance

ebereplenty

NJOKU SAMSON EBERE

Posted on March 6, 2020

Algorithm 101: 3 Ways to Find Hamming Distance

According to Wikipedia, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimum number of substitutions required to change one string into the other, or the minimum number of errors that could have transformed one string into the other.


hammingDistance("part", "path"); // 2

hammingDistance("ebere", "samson"); // 'unequal word lengths'

Enter fullscreen mode Exit fullscreen mode

In how many ways can you check for the hamming distance of two given strings of equal length? When was the last time you used do...while...loop and while...loop?

This article presents us with 3 ways to check for hamming distance. Let's get to it.

Prerequisite

This article assumes that you have basic understanding of javascript's string and array methods.

Let's determine the hamming distance of two given strings using:

  • for...loop, if...statement and toLowerCase()
      function hammingDistance(word, matchingWord) {
        let count = 0;

        if (word.length === matchingWord.length) {
          for (let i = 0; i <= word.length; i++) {
            if (word.toLowerCase()[i] !== matchingWord.toLowerCase()[i]) {
              count++;
            }
          }

          return count;
        }
        return "unequal word lengths";
      }
Enter fullscreen mode Exit fullscreen mode
  • while...loop, if...statement and toLowerCase()
      function hammingDistance(word, matchingWord) {
        let count = 0;
        let i = 0;

        if (word.length === matchingWord.length) {
          while (i <= word.length) {
            if (word.toLowerCase()[i] !== matchingWord.toLowerCase()[i]) {
              count++;
            }
            i++;
          }

          return count;
        }
        return "unequal word lengths";
      }
Enter fullscreen mode Exit fullscreen mode
  • do...while...loop if...statement and toLowerCase()
      function hammingDistance(word, matchingWord) {
        let count = 0;
        let i = 0;

        if (word.length === matchingWord.length) {
          do {
            if (word.toLowerCase()[i] !== matchingWord.toLowerCase()[i]) {
              count++;
            }
            i++;
          } while (i <= word.length);

          return count;
        }
        return "unequal word lengths";
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 6 Ways to Find the Longest Word in a Sentence

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

💖 💪 🙅 🚩
ebereplenty
NJOKU SAMSON EBERE

Posted on March 6, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related