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
0n
1a
2g
3r
4a
5m
6n
7a
8g
9a
10r
11a
12m
13Mapcount
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.
Variables
phase=setup
How to think about it
- Different lengths? Bail immediately — can't be shuffles.
- Count how often each letter appears in s.
- Walk t: for each letter, try to spend one from the tally.
- Any letter missing in the tally means mismatch.
- 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 1
true{"s":"anagram","t":"nagaram","values":["a","n","a","g","r","a","m","n","a","g","a","r","a","m"]}
Same letters, different order.
Example 2
false{"s":"rat","t":"car","values":["r","a","t","c","a","r"]}
't' is missing in car; 'c' is missing in rat.
Example 3
false{"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