CodeArchon

easyArrays & HashingAmazonMicrosoftMeta

Letter Shuffle Verifier

Two gift tags arrive. Are they rearrangements of the same letters, or did someone mislabel a box?

Given two strings `s` and `t`, decide whether `t` is a rearrangement of the letters in `s`. The strings are letter shuffles when they have the same length and every letter appears the same number of times in each.

Choose approach

a
0
n
1
a
2
g
3
r
4
a
5
m
6
n
7
a
8
g
9
a
10
r
11
a
12
m
13
Mapcount
empty
1/23
Narrationstep 1 of 23

Open an empty letter tally. Phase 1: count every letter in s. Phase 2: cancel each letter of t against the tally.

1def is_letter_shuffle(s: str, t: str) -> bool:
2 if len(s) != len(t):
3 return False
4 count: dict[str, int] = {}
5 for ch in s:
6 count[ch] = count.get(ch, 0) + 1
7 for ch in t:
8 if count.get(ch, 0) == 0:
9 return False
10 count[ch] -= 1
11 return True
Variables
phase=setup

How to think about it

  1. Different lengths? Bail immediately — can't be shuffles.
  2. Count how often each letter appears in s.
  3. Walk t: for each letter, try to spend one from the tally.
  4. Any letter missing in the tally means mismatch.
  5. A clean pass means every letter balanced — they're shuffles.

Edge cases to catch in interview

  • Different lengths → immediate false.
  • Both empty strings → vacuously true.
  • All same letter — tally hits zero mid-pass if there are extras.
  • Treat case-sensitivity explicitly: 'A' and 'a' are different here.

Examples

Example 1true
{"s":"anagram","t":"nagaram","values":["a","n","a","g","r","a","m","n","a","g","a","r","a","m"]}

Same letters, different order.

Example 2false
{"s":"rat","t":"car","values":["r","a","t","c","a","r"]}

't' is missing in car; 'c' is missing in rat.

Example 3false
{"s":"a","t":"ab","values":["a","a","b"]}

Lengths disagree.

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed