# Leetcode – Find the Difference Solution in Python

## Introduction

In this article, we will look at the “Find the Difference” problem on Leetcode and explores various methods for solving it using Python.

## Problem Statement

You are given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then adding one more letter at a random position.

Find the letter that was added in t.

## Analyzing the Problem

This problem requires us to find the extra character in string t which is not present in string s. Since t is formed by shuffling s and adding one character, the problem boils down to identifying that extra character.

Let’s explore different strategies for solving this problem:

## Approach 1: Using Hash Tables

One way to solve this problem is to use hash tables to keep track of the frequency of each character in both strings. By comparing the frequency of characters in t with the frequency in s, we can easily find the extra character.

def findTheDifference(s: str, t: str) -> str:
char_count_s = {}
char_count_t = {}

# Count the frequency of each character in string s
for char in s:
char_count_s[char] = char_count_s.get(char, 0) + 1

# Count the frequency of each character in string t
for char in t:
char_count_t[char] = char_count_t.get(char, 0) + 1

# Find the character that has a higher count in t than in s
for char, count in char_count_t.items():
if count > char_count_s.get(char, 0):
return char

This approach has a time complexity of O(N) and a space complexity of O(1), as there are only 26 lowercase letters and the size of the hash tables is constant.

## Approach 2: Using Collections Counter

Python’s collections module provides a Counter class, which makes it easier and more concise to count the elements in an iterable. We can utilize the Counter class to simplify our solution.

from collections import Counter

def findTheDifference(s: str, t: str) -> str:
# Count the characters in s and t
count_s = Counter(s)
count_t = Counter(t)

# Find the character with a different count in t
for char, count in count_t.items():
if count > count_s[char]:
return char


This approach has the same time and space complexity as the first approach but is cleaner due to the use of the Counter class.

## Approach 3: Using Bit Manipulation

We can also solve this problem using bit manipulation. By XOR-ing all the characters of s and t together, the result will be the extra character. This is because a XOR a = 0 and 0 XOR b = b.

def findTheDifference(s: str, t: str) -> str:
result = 0

# XOR all characters in s
for char in s:
result ^= ord(char)

# XOR all characters in t
for char in t:
result ^= ord(char)

# The final result is the ASCII of the extra character
return chr(result)

This approach has a time complexity of O(N) but has a constant space complexity of O(1), as it uses a single integer to keep track of the XOR result.

## Approach 4: Using Summation

Another approach to solve this problem is by taking the sum of ASCII values of characters in t and subtracting the sum of ASCII values of characters in s. The result will be the ASCII value of the extra character.

def findTheDifference(s: str, t: str) -> str:
return chr(sum(map(ord, t)) - sum(map(ord, s)))

This approach also has a time complexity of O(N) and a space complexity of O(1).

## Conclusion

We’ve dissected the “Find the Difference” problem from Leetcode and perused a range of approaches in Python. Each approach has its merits, with some being more intuitive and others more optimized. Selecting the right approach depends on various factors including the constraints of the problem and the performance requirements. Bit manipulation and summation approaches are more optimal in terms of space complexity, whereas using hash tables can be more intuitive to understand for someone reading the code.