Letter twins
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
Two words are letter twins if you can make one by rearranging all the letters of the other, using every letter exactly once. For example, listen and silent are letter twins.
Given two words, first and second, return true if they are letter twins, and false if they are not.
Examples
Example 1
- Input
- first = "listen", second = "silent"
- Output
- true
Both have one each of e, i, l, n, s and t.
Example 2
- Input
- first = "apple", second = "paple"
- Output
- true
Both have two p's and one each of a, l and e.
Example 3
- Input
- first = "aab", second = "abb"
- Output
- false
Same letters, but different amounts of each: two a's against one.
Rules
- Both words use only lowercase letters a to z.
- Each word has between 0 and 50,000 letters.
- A word counts as a letter twin of itself.
Words on this page, in plain English (1)Open if any word is new to you
- Boolean (true or false)
- A value that can only be
trueorfalse. Like a light switch: on or off.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
Two words are letter twins when they use exactly the same letters, the same number of times, just in a different order. listen and silent are twins. aab and abb are not: both use a and b, but in different amounts.
2. Solve an example by hand
How would you check apple and paple on paper? You might tally the letters of apple: a 1, p 2, l 1, e 1. Then cross off one tally for each letter of paple. If every tally reaches exactly zero, they're twins.
That tally is a count, and counting is one of the three hashing templates.
3. The slow way
One idea: for each letter of first, search second for a matching letter that hasn't been used yet, and cross it out. Each search scans the second word, so it's O(n²): about 2.5 billion steps for two 50,000-letter words.
Another idea is to sort both words and compare: "listen" and "silent" both sort to "eilnst".
function areLetterTwins(first, second) { const sort = (word) => [...word].sort().join(""); return sort(first) === sort(second);}That's short and correct, and O(n log n) because of the sorting. Good enough to pass! But we can do better, and the better way teaches the counting template.
4. Spot the pattern
The question is really: "does every letter appear the same number of times in both?" "How many times does each thing appear?" is the signal for count, then answer.
5. The faster idea
- If the lengths differ, they can't be twins. Stop.
- Go through
firstand count up: add 1 for each letter. - Go through
secondand count down: take 1 away. If a letter's count is already 0,secondhas more of it thanfirst, so they're not twins. - If we get through
secondwithout that happening, they're twins.
Count up, then count down
first
second
counts (Set)
empty
function areLetterTwins(first, second) { if (first.length !== second.length) { return false; } const counts = new Map(); // letter → how many more in first than in second for (const letter of first) { counts.set(letter, (counts.get(letter) ?? 0) + 1); } for (const letter of second) { const left = counts.get(letter) ?? 0; if (left === 0) { return false; } counts.set(letter, left - 1); } return true;}Same length, so they might be twins. Start with an empty Map of letter counts.
Why don't we need to check that all counts end at zero? Because the words have the same length: if second never takes a count below zero, it must use up every letter exactly.
6. The code, line by line
if (first.length !== second.length) { return false; }A quick early exit: words of different lengths can never be twins.
const counts = new Map(); // letter → how many more in first than in second for (const letter of first) { counts.set(letter, (counts.get(letter) ?? 0) + 1); }Count every letter of
first.(counts.get(letter) ?? 0) + 1means "the count so far, or 0 if it's new, plus one".for (const letter of second) { const left = counts.get(letter) ?? 0; if (left === 0) { return false; }For each letter of
second, look up how many are left. If none are left,seconduses this letter more times thanfirstdoes: not twins.counts.set(letter, left - 1);Otherwise, use one up.
return true;Every letter of
secondwas matched. Twins!
7. How fast is it?
| Search and cross out | Sort and compare | Count | |
|---|---|---|---|
| Time | O(n²) | O(n log n) | O(n) |
| Extra memory | O(n) | O(n) | O(1): at most 26 letters |
Here the Map can only ever hold 26 keys, one per letter, no matter how long the words are. Memory that doesn't grow with the input counts as O(1).
Common mistakes
- Only checking which letters appear.
aabandabbuse the same letters, so a Set would say "twins". Amounts matter: use counts. - Using
includesandreplace. Removing letters from a string one by one rebuilds the string each time, which is slow for long words.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Letter twins
Read the 17 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function areLetterTwins(first, second) { if (first.length !== second.length) { return false; } const counts = new Map(); // letter → how many more in first than in second for (const letter of first) { counts.set(letter, (counts.get(letter) ?? 0) + 1); } for (const letter of second) { const left = counts.get(letter) ?? 0; if (left === 0) { return false; } counts.set(letter, left - 1); } return true;}