Find the Difference

Try to solve the Find the Difference problem.

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.

Examples#

Created with Fabric.js 3.6.6 Input 1 Sample example 1 string 1 m p o n string 2 m n o The extra character "p" is only present in string 1, whose index is 1.

1 of 4

Created with Fabric.js 3.6.6 Input 3 Sample example 2 string 1 w x y z string 2 w x y t z The extra character "t" is only present in string 2, whose index is 3.

2 of 4

Created with Fabric.js 3.6.6 Input 0 Sample example 3 string 1 h e l l o string 2 n h e l l o The extra character "n" is only present in string 2, whose index is 0.

3 of 4

Created with Fabric.js 3.6.6 Input 0 string 1 a a a a a Sample example 4 string 2 a a a a a a The extra character "a" is only present in string 2, whose index is 0.

4 of 4

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Find the Difference

1

Find the index of the extra character that is present in only one of the strings.

string 1 = “pqr”

string 2 = “psrq”

A)

0

B)

1

C)

2

D)

3

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Intialize a variable, result, with 00.

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.

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.

result now contains the ASCII value of the extra character. Find and return the index of the extra character from the longer string.


Try it yourself#

Implement your solution in the following coding playground.

Python
usercode > main.py
Input #1
Input #2
Find the Difference

Bitwise Manipulation: Introduction

Solution: Find the Difference