Scrambled Code Hidden in a Log
Medium · Sliding Window · about 20 min
A secret code (lowercase letters, at least one) may have been leaked into a long server log (lowercase letters) with its letters shuffled.
Return True if log contains a block of back-to-back letters that uses exactly the letters of code, each the same number of times (any order). Otherwise return False.
Examples:
has_shuffled_block("ab", "eidbaooo") → True ("ba")
has_shuffled_block("ab", "eidboaoo") → False (the b and a are not next to each other)
Aim for O(n) where n is the length of the log.
Hints
- A shuffled copy has the same length and the same count of every letter.
- Slide a window exactly as long as the code, updating counts for the letter that enters and the one that leaves.
- Compare counts after every slide, including the very first full window and the very last one.
Learn the pattern first: the Sliding Window lesson.
More Sliding Window problems
- Food Stall Moving Average (Easy)
- Badge Scanned Twice Too Soon (Easy)
- Longest Stretch With No Repeat Song (Medium)
- Repaint Tiles for the Longest Run (Medium)
- Fewest Orders to Hit the Goal (Medium)
- Shortest Note That Covers Every Letter (Hard)
- Rolling Peak of a Sensor (Hard)