### Find the Difference

## Problem

Given two strings ** s** and

**which consist of only lowercase letters.**

*t*String ** t** is generated by random shuffling string

**and then add one more letter at a random position.**

*s*Find the letter that was added in ** t**.

**Example:**

Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.

## Solution

For problems of finding the difference, first think about XOR operation. Two points about XOR must be comfortable with:

- 0 ^ A = A
- XOR is commutable, so sequence not matters

Secondly, think about the length of s and t. t must have one more letter than s. Combined with the commutation of XOR, two XOR operations can be put under the same for loop.

Advertisements