461. Hamming Distance

461. Hamming Distance

In this article we are going through the Leet Code Hamming Distance problem. We are given 2 integer numbers and we are told to find the distance from the bits that differ from each other. A good way to formulate the answer is first write the bits of each byte.


Here we have a 4 byte representation. We are being asked to count the distance by marking the position. Starting form the less significant bit we see that 1 and 1 are same but moving one position to left 1 and 0 is not the same. The rest are all the same. Therefore, the hamming distance is 1.


Using XOR Bitwise Operator


If we use the Xor bitwise operator for the the opposite bits will get 1 and for same will get 0. This solution is pefect since we can generate a 4 bits where the 1 mark the positions we looking for. Thus, we just return the number of 1's as the hamming distance.




/**
 * @param {number} x
 * @param {number} y
 * @return {number}
 */
var hammingDistance = function (x, y) {

    let byte = Array.from((x ^ y).toString(2).padStart(4, 0))

    let hammingDistance = 0


    for (let bit of byte) {
        if (bit === "1") {
            hammingDistance += 1
        }
    }

    return hammingDistance

};        


https://leetcode.com/problems/hamming-distance/solutions/5827049/using-xor-operator

要查看或添加评论,请登录

Mohamed Sharif的更多文章

  • An Easier explanation for AVL Trees And Implemented Using Python

    An Easier explanation for AVL Trees And Implemented Using Python

    How To Find Parent And Children When An Array Is Given AVL trees might seem more complicated than what it seems but if…

  • Implementing A Binary Heap Using Python

    Implementing A Binary Heap Using Python

    Binary heaps are a form of binary tree but the only order that we follow is that the parent value should be either…

  • Identifying Node's Relations From Array For A Complete Binary Tree

    Identifying Node's Relations From Array For A Complete Binary Tree

    I would like to make this article more conceptual and to revise the relation of the current node value to its left…

  • Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    This article can be better understood if you are an intermediate React and familiar with API Calls. In this article, I…

  • 1064. Fixed Point

    1064. Fixed Point

    https://leetcode.com/problems/fixed-point/solutions/5862817/use-binary-search-and-if-value-found-recurse-to-the-left-sin…

  • Odd Even Linked List

    Odd Even Linked List

    In this article I would like to go over a solution to solve leet code problem 328. Odd Even Linked List.

  • 1122. Relative Sort Array

    1122. Relative Sort Array

    In this problem we are given 2 arrays where the first array contains all the numbers that the second array has but the…

  • Leaf-Similar Trees

    Leaf-Similar Trees

    I will be walking through the algorithm to solve the problem of 2 nodes where we are told to find if the leaf of both…

  • Solving Leet Code 243. Shortest Word Distance

    Solving Leet Code 243. Shortest Word Distance

    In this problem, we are given a array with words and there are 2 words that already exist and the goal is to return the…

  • How To know If A Word Is Palindrome

    How To know If A Word Is Palindrome

    So many do not know and I would said again many do not know what is a palindrome. Why would anyone outside software…

    1 条评论

社区洞察

其他会员也浏览了