Solution: Find the Difference
Let's solve the Find the Difference problem using the Bitwise Manipulation pattern.
Statement#
Given two strings, str1 and str2, find the index of the extra character that is present in only one of the strings.
Note: If multiple instances of the extra character exist, return the index of the first occurrence of the character in the longer string.
Constraints:
-
str1.length,str2.length - Either
str2.lengthstr1.length + 1, or,str1.lengthstr2.length + 1 - The strings consist of lowercase English letters.
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
The naive approach is to first sort both the strings. Then loop over the longer string and compare both strings, character by character. Finally, when one extra character is found in the longer string which does not match with the character at the corresponding index in the other string, we break out of the loop and return the index where the comparison failed.
The time complexity of this solution would be , that is . Here, is the cost of sorting one of the strings. The space complexity would be .
Optimized approach using bitwise manipulation#
An optimized approach to solve this problem is using the bitwise manipulation technique. We use the bitwise XOR operation to identify the extra character from one of the strings and return the index of that character.
The algorithm proceeds through the following steps:
-
Initialize a variable,
result, with to store the XOR result. -
Find the lengths of both strings.
-
Iterate over the characters of
str1, and for each character, do the following operations:- Compute the ASCII value of the character.
- Perform a bitwise XOR operation between the current value of
resultand the ASCII value. - Store the result of the bitwise XOR operation back in
result.
-
Now, iterate over the characters of
str2and repeat the operations performed in step 3 for each character instr2. -
After iterating over both strings, the
resultvariable will correspond to the ASCII value of the extra character. -
Find and return the index of the extra character from the string with a greater length.
The slides below illustrate how we would like the algorithm to run:
1 of 16
2 of 16
3 of 16
4 of 16
5 of 16
6 of 16
7 of 16
8 of 16
9 of 16
10 of 16
11 of 16
12 of 16
13 of 16
14 of 16
15 of 16
16 of 16
Note: In the following section, we will gradually build the solution. Alternatively, you can skip straight to just the code.
Step-by-step solution construction#
Let’s start with the simplest step:
Initialize a variable result with 0. Find the lengths of both strings. Iterate over each character of str1, and for each character, perform a bitwise XOR operation between the current value of result and the ASCII value of the character. Update the result with the computed XOR value every time.
Similar to the first step, now iterate over each character of the str2, and for each character, perform the bitwise XOR operation between the current value of the result and the ASCII value of the character. Again, update the result variable with the computed XOR value every time. After the traversals of both strings, result will contain the ASCII value of the extra character.
This technique works because XOR returns if the bits are the same (both or both ) and if the bits differ (one is , and the other is ). For example, returns , and returns . Keeping this property in mind, when we traverse the str2, the common characters between the str1 and str2 cancel out, leaving only the extra character in the result variable.
Now, we have the ASCII value of the extra character in the result variable. To find the index of this extra character, we check the length of both strings. Since the extra character is present in the longest string, we return the index of that extra character from the longest string.
Just the code#
Here’s the complete solution to this problem:
Solution summary#
To recap, the solution to this problem can be divided into the following three parts:
-
Intialize a variable,
result, with . -
Perform a bitwise XOR operation between the current value of
resultand the ASCII value of each character instr1. Update the value ofresultwith the computed XOR value every time. -
Perform a bitwise XOR operation between the current value of
resultand the characters ofstr2. Update the value ofresulteach time with the computed XOR value. -
resultnow contains the ASCII value of the extra character. Find and return the index of the extra character from the longer string.
Time complexity#
The time complexity of the solution is , where is the length of the longest string.
Space complexity#
The space complexity of the solution above is , because we used a constant amount of space.
Find the Difference
Complement of Base 10 Number