Javascript Solution Find the Difference
In this article we will solve leetcode problem `Find the Difference' using Bit Manipulation.
Problem Overview
There are two string s
and t
.
String t
is generated by random shuffling string s
and then add one more letter at a random position.
We have to return added letter.
For exact problem visit Leetcode Link;
Sample Input Output
Javascript Solution
Essentially we will be taking advantage of the Bitwise XOR ^ operator. For every NUMBER X, we have
X^X=0 and X^0=X So the plan is:
- Iterate over the shortest array
s
- Keep accumulating (XOR ^ing) the elements of both
s
andt
, after we have converted them to Numbers with CharCodeAt() so the XOR can work. Elements that appear both int
ands
will give us zero (X^X=0
), yet that zero will not affect the total sum (X^0=X
) - Return the converted back to character final sum, which is essentially the (desired) character not present in s
Original Solution Link GeorgeChryso;
Same Algorithm Implementation Using ES6
More Articles
Deploying Next.js Apps
How to deploy your Next.js apps on Vercel.
January 2, 2023
How to show CHANGELOG page in storybook of react, typescript project
In this blog post, we'll go over the steps for showing a CHANGELOG page in your Storybook instance. By the end, you'll have a clear understanding of how to set this up and keep your CHANGELOG up to date.
December 20, 2022