Hidden shuffle
medium
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.
Given a short word word and a longer text, check whether text contains a stretch of letters in a row that uses exactly the same letters as word, in any order.
For example, text = "xcbayz" contains "ba", a shuffle of word = "ab", so the answer is true.
Return true or false.
Examples
Example 1
- Input
- word = "ab", text = "xcbayz"
- Output
- true
"ba" (positions 2 and 3) uses the same letters as "ab".
Example 2
- Input
- word = "ab", text = "xbcayz"
- Output
- false
The a and b are there, but not next to each other.
Example 3
- Input
- word = "listen", text = "xxsilentxx"
- Output
- true
Rules
- Both strings use only lowercase letters a to z.
wordhas between 1 and 10,000 letters;texthas between 1 and 100,000.
Words on this page, in plain English (2)Open if any word is new to you
- String
- A piece of text, written between quotes. Each letter in it is a character.
- 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
Somewhere in text, is there a stretch of letters in a row that is a shuffle of word? A shuffle uses exactly the same letters the same number of times: it's a letter twin.
2. Solve an example by hand
word = "ab", text = "xcbayz". A shuffle of "ab" is 2 letters long, so only 2-letter stretches matter: xc, cb, ba… and ba is a shuffle of ab. Answer: true.
3. The slow way
For every stretch of word.length letters, check whether it's a letter twin of word by counting (or sorting) its letters. That's n stretches × k letters each: O(n × k). With a 100,000-letter text and a 10,000-letter word, that's a billion steps.
4. Spot the pattern
Every stretch has the same fixed length, and neighbouring stretches differ by one letter in and one letter out: a fixed-size sliding window. What we track inside the window is letter counts, like in Letter twins.
5. The window idea
- Count the letters of
word. - Count the letters of the first window (the first
word.lengthletters oftext). Same counts? Found it. - Slide: add the letter coming in, remove the one going out, compare again.
Slide a word-sized window and compare counts
text
wordCounts (Map)
| letter | count |
|---|---|
| "a" | 1 |
| "b" | 1 |
| "c" | 1 |
windowCounts (Map)
| letter | count |
|---|---|
| "a" | 0 |
| "b" | 1 |
| "c" | 1 |
| "e" | 1 |
function hasHiddenShuffle(word, text) { if (word.length > text.length) { return false; } const wordCounts = countLetters(word); const windowCounts = countLetters(text.slice(0, word.length)); if (sameCounts(wordCounts, windowCounts)) { return true; } for (let right = word.length; right < text.length; right++) { windowCounts[indexOf(text[right])]++; // letter comes in windowCounts[indexOf(text[right - word.length])]--; // letter goes out if (sameCounts(wordCounts, windowCounts)) { return true; } } return false;} // "a" → 0, "b" → 1, … "z" → 25function indexOf(letter) { return letter.charCodeAt(0) - "a".charCodeAt(0);} // 26 counts, one per letterfunction countLetters(text) { const counts = new Array(26).fill(0); for (const letter of text) { counts[indexOf(letter)]++; } return counts;} function sameCounts(a, b) { return a.every((count, i) => count === b[i]);}Count the first 3 letters, "cbe". Not the same counts as the word.
6. The code, line by line
if (word.length > text.length) { return false; }A word longer than the text can't hide in it.
const wordCounts = countLetters(word); const windowCounts = countLetters(text.slice(0, word.length)); if (sameCounts(wordCounts, windowCounts)) { return true; }Count the word's letters and the first window's letters. Maybe the very first window already matches.
for (let right = word.length; right < text.length; right++) { windowCounts[indexOf(text[right])]++; // letter comes in windowCounts[indexOf(text[right - word.length])]--; // letter goes outSlide the window:
text[right]joins, and the letterword.lengthplaces back leaves.if (sameCounts(wordCounts, windowCounts)) { return true; }After each slide, compare the counts.
// "a" → 0, "b" → 1, … "z" → 25function indexOf(letter) { return letter.charCodeAt(0) - "a".charCodeAt(0);}A trick for counting letters: give each one a number from 0 to 25 by subtracting the code of
"a". Then an array of 26 counts is all we need.// 26 counts, one per letterfunction countLetters(text) { const counts = new Array(26).fill(0); for (const letter of text) { counts[indexOf(letter)]++; } return counts;}Count every letter of a piece of text into the 26 slots.
function sameCounts(a, b) { return a.every((count, i) => count === b[i]);}Two count arrays match when every slot matches.
everystops at the first difference.
7. How fast is it?
| Check every stretch | Sliding window | |
|---|---|---|
| Time | O(n × k) | O(26 × n) = O(n) |
| Extra memory | O(26) | O(26) = O(1) |
You can go further and keep a running count of how many letters differ, so each step is O(1) instead of O(26). It's a nice optimisation, but O(26 × n) is already O(n), and much easier to get right.
Common mistakes
- Checking only after sliding. The very first window needs checking too.
- Comparing arrays with
===. Two different arrays are never===, even with the same contents. Compare slot by slot.
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: Hidden shuffle
Read the 31 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 hasHiddenShuffle(word, text) { if (word.length > text.length) { return false; } const wordCounts = countLetters(word); const windowCounts = countLetters(text.slice(0, word.length)); if (sameCounts(wordCounts, windowCounts)) { return true; } for (let right = word.length; right < text.length; right++) { windowCounts[indexOf(text[right])]++; // letter comes in windowCounts[indexOf(text[right - word.length])]--; // letter goes out if (sameCounts(wordCounts, windowCounts)) { return true; } } return false;} // "a" → 0, "b" → 1, … "z" → 25function indexOf(letter) { return letter.charCodeAt(0) - "a".charCodeAt(0);} // 26 counts, one per letterfunction countLetters(text) { const counts = new Array(26).fill(0); for (const letter of text) { counts[indexOf(letter)]++; } return counts;} function sameCounts(a, b) { return a.every((count, i) => count === b[i]);}