DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

4 2

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay