Find the Difference

You are given two strings s and t. String t is generated by randomly shuffling string s and then adding one more letter at a random position. Return the letter that was added to t.

30:00

Find the Difference
easy
Free written solution
Topics
Companies

You are given two strings s and t. String t is generated by randomly shuffling string s and then adding one more letter at a random position. Return the letter that was added to t.

Example 1:
Input: {"s":"abcd","t":"abcde"}
Output: e
Constraints:
  • 0≤s.length≤10000 \leq s.length \leq 1000

  • t.length=s.length+1t.length = s.length + 1

  • ss and tt consist of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abcd","t":"abcde"}

t is s shuffled with one extra letter. Start with x = 0 and XOR in every character code of s and then of t. Pairs cancel because c ^ c = 0.

s (4)

a
b
c
d

t (5)

a
b
c
d
e

Accumulator x (low 8 bits)

x
0
0
0
0
0
0
0
0
0

letters seen an odd number of times (x is their XOR)

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Variables
VariableValue
x0
DepthFunction Call
Stack empty
0/12