Skip to content
Algorithms for Beginners

Problems / Sliding window

Hidden shuffle

medium

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. 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

  1. Example 1

    Input
    word = "ab", text = "xcbayz"
    Output
    true

    "ba" (positions 2 and 3) uses the same letters as "ab".

  2. Example 2

    Input
    word = "ab", text = "xbcayz"
    Output
    false

    The a and b are there, but not next to each other.

  3. Example 3

    Input
    word = "listen", text = "xxsilentxx"
    Output
    true

Rules

  • Both strings use only lowercase letters a to z.
  • word has between 1 and 10,000 letters; text has 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 true or false. Like a light switch: on or off.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function hasHiddenShuffle(word, text) {
  // Return true or false.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in sliding window

  • Learn the sliding window pattern

    Keep a stretch of the list and slide it along instead of starting over.

  • Two kinds

    Medium. Find the longest stretch of a list that contains at most two different values.

  • Best streak

    Easy. Find the highest total of any k days in a row.

  • Longest unique stretch

    Medium. Find the longest stretch of text in which no character repeats.

  • Shortest to target

    Medium. Find the fewest days in a row whose rainfall adds up to at least a target.

  • Repaint the fence

    Medium. Repaint at most k fence boards to make the longest possible stretch of one colour.