Same Letter Tiles
In a word game, you lay down listen. Your friend shuffles your six tiles and lays down silent: same tiles, new order, a legal move.
Next round she tries to turn your teeth into teethe, which needs a sixth tile she does not have. You want a quick check for any claimed rearrangement.
Your task
Write sameTiles(word, rearranged). Return true if rearranged uses exactly the same letters as word, each the same number of times, in any order. Otherwise return false.
Both strings contain only lowercase letters a to z. Two empty strings count as the same tiles.
sameTiles(word: string, rearranged: string) → bool
Examples
word = "listen" rearranged = "silent"
true
Both words have one each of e, i, l, n, s and t, so they use the same tiles in a different order.
word = "teeth" rearranged = "teethe"
false
The lengths differ (5 against 6), so the word cannot be a rearrangement and there is no need to compare letters.
word = "brag" rearranged = "garb"
true
Both words contain exactly the letters b, r, a and g, so the tiles were shuffled with none added and none left over.
Constraints
- 0 ≤ word.length, rearranged.length ≤ 50,000
- Only lowercase English letters.
Start with brute force
For each letter of word, find a matching unused tile in rearranged and cross it off. If a letter has no matching tile, the answer is false.
Each search scans the second word: O(n²), over a billion steps for 50,000 letters. Sorting both words and comparing is O(n log n), which is fine, but counting is simpler and faster.
Hints
0 of 4 shown- Hint 1
aabandabbuse the same set of letters. Why are they still not the same tiles? - Hint 2
What matters is how many of each letter there are. Two words are the same tiles exactly when every letter has the same count in both.
- Hint 3
Count the letters of the first word. How could the second word "use up" those counts, and what would you check?
- Hint 4
If the lengths differ, return
false. Otherwise build a map letter → count fromword, then walkrearrangedsubtracting one per letter. If any count drops below zero, returnfalse. Otherwise returntrue.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Only the count of each letter matters. So the code counts the first word's tiles, and then the second word uses them up one at a time.
Trace teeth against tethe, which both have length 5:
- Counting
teethgives t → 2, e → 2, h → 1. - Using
tgives t → 1, usingegives e → 1, and usingtagain gives t → 0. - Using
hgives h → 0, and usingegives e → 0. - No count went below zero, so every tile was matched. Return
true.
Checking for negative counts is enough because the lengths are equal, so if some letter were short, another would be over-used to make up the length, and its count would go negative.
function sameTiles(word, rearranged) {
if (word.length !== rearranged.length) return false;
const count = new Map(); // letter -> tiles still unused
for (const ch of word) count.set(ch, (count.get(ch) || 0) + 1);
for (const ch of rearranged) {
const left = (count.get(ch) || 0) - 1;
if (left < 0) return false; // no tile left for this letter
count.set(ch, left);
}
return true;
}def same_tiles(word, rearranged):
if len(word) != len(rearranged):
return False
count = {} # letter -> tiles still unused
for ch in word:
count[ch] = count.get(ch, 0) + 1
for ch in rearranged:
left = count.get(ch, 0) - 1
if left < 0: # no tile left for this letter
return False
count[ch] = left
return TrueRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.