Solution: Find the Difference

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:

  • 00 \leq str1.length, str2.length 1000\leq 1000
  • Either str2.length == str1.length + 1, or, str1.length == str2.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 O(nlogn+n)O(n \log n + n), that is O(nlogn)O(n \log n). Here, O(nlogn)O(n \log n) is the cost of sorting one of the strings. The space complexity would be O(1)O(1).

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:

  1. Initialize a variable, result, with 00 to store the XOR result.

  2. Find the lengths of both strings.

  3. 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 result and the ASCII value.
    • Store the result of the bitwise XOR operation back in result.
  4. Now, iterate over the characters of str2 and repeat the operations performed in step 3 for each character in str2.

  5. After iterating over both strings, the result variable will correspond to the ASCII value of the extra character.

  6. 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:

Created with Fabric.js 3.6.6 Given the above two strings. Perform XOR of result variable on both of the strings one by one to find the index of the extra character. Initially the result is assigned 0. After performing XOR on both ofthe strings, result will hold the ASCII of the extra character. result = 0 c o u r a e c o u r a g e

1 of 16

Created with Fabric.js 3.6.6 result = 0 c o u r a e result = 0

2 of 16

Created with Fabric.js 3.6.6 c o u r a e

3 of 16

Created with Fabric.js 3.6.6 c o u r a e

4 of 16

Created with Fabric.js 3.6.6 c o u r a e

5 of 16

Created with Fabric.js 3.6.6 c o u r a e

6 of 16

Created with Fabric.js 3.6.6 c o u r a e

7 of 16

Created with Fabric.js 3.6.6 c o u r a e

8 of 16

Created with Fabric.js 3.6.6 result = 0 c o u r a g e result = 15

9 of 16

Created with Fabric.js 3.6.6 c o u r a g e

10 of 16

Created with Fabric.js 3.6.6 c o u r a g e

11 of 16

Created with Fabric.js 3.6.6 c o u r a g e

12 of 16

Created with Fabric.js 3.6.6 c o u r a g e

13 of 16

Created with Fabric.js 3.6.6 c o u r a g e

14 of 16

Created with Fabric.js 3.6.6 c o u r a g e

15 of 16

Created with Fabric.js 3.6.6 c o u r a g e The result = 103, which is the ASCII of "g".The index of "g" is 5.

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.

Find the Difference

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 00 if the bits are the same (both 00 or both 11) and 11 if the bits differ (one is 00, and the other is 11). For example, 111 XOR 111111 \space XOR \space 111 returns 000000, and 101 XOR 010101 \space XOR \space 010 returns 111111. 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.

Find the Difference

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.

Find the Difference

Just the code#

Here’s the complete solution to this problem:

Find the Difference

Solution summary#

To recap, the solution to this problem can be divided into the following three parts:

  1. Intialize a variable, result, with 00.

  2. Perform a bitwise XOR operation between the current value of result and the ASCII value of each character in str1. Update the value of result with the computed XOR value every time.

  3. Perform a bitwise XOR operation between the current value of result and the characters of str2. Update the value of result each time with the computed XOR value.

  4. result now 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 O(n)O(n), where nn is the length of the longest string.

Space complexity#

The space complexity of the solution above is O(1)O(1), because we used a constant amount of space.

Find the Difference

Complement of Base 10 Number