</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Rolling Dictionary

What is this?

Sometimes you don't just care how big the window is โ€” you care exactly which letters are inside it. So as the window slides like a spotlight across a string, you keep a little tally of each character it covers. When a letter slides in you bump its count up; when one slides out you bump it down. Checking whether the window matches some target then becomes a quick glance at the tally instead of re-counting from scratch.

flowchart TD A["Window slides over the string"] --> B["A letter enters so bump its count up"] A --> C["A letter leaves so bump its count down"] B --> D["Compare the tally to the target"] C --> D

๐Ÿ’ก Fun fact: This rolling-count trick is the backbone of fast text search in tools like grep, and a close cousin โ€” the rolling hash โ€” powers plagiarism detectors and Git's own delta compression by fingerprinting sliding chunks of data.

๐Ÿ”“ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: When a window problem is about which characters are inside (not just a sum), carry a rolling frequency map that you update one-in / one-out as the window slides. Comparing that map to a target tells you, in O(1) with a match-counter, whether the window is an anagram / permutation / exact match โ€” turning repeated re-counting into a single pass.


The move

The move: maintain count[] for the window; slide with count[entering] += 1 and count[leaving] -= 1; valid when window counts == target counts (track a match-counter for O(1))

The subtlety is what you compare: a multiset match (anagram/permutation โ€” order doesn't matter) versus an ordered match (exact substring search โ€” order does matter).


The problems

Rolling dictionary branches into Find All Anagrams (multiset, all indices), Permutation in String (multiset, exists?) and First Occurrence / strStr (ordered match)

  • Find All Anagrams in a String โ€” fixed window, return every index whose count map equals the pattern's.
  • Permutation in String โ€” same engine as an existence check with early exit.
  • Find the Index of the First Occurrence โ€” the contrast: ordered matching (use a sliding compare, or rolling-hash / KMP), not a multiset.

Key takeaways

  • Carry a count map and update it incrementally โ€” one in, one out per slide.
  • A match-counter compares two maps in O(1) (track how many letters are at the target count).
  • Multiset vs ordered: anagrams/permutations ignore order; substring search needs exact order.
  • Why interviewers love it: it tests whether you maintain derived state incrementally instead of recomputing.

Start here: Find All Anagrams in a String, then Permutation in String.

Find the Index of the First Occurrence in a String

Core idea: Slide a window of length len(needle) across haystack and compare each window to the needle. The twist that sets this apart from the anagram problems in this chapter: order matters. An anagram window asks "do I have the right multiset of characters?"; here we ask "do I have the exact same sequence, character for character, in the same order?" Same sliding frame, stricter question.

Problem, rephrased

You're building the "Find" feature for a text editor (Ctrl+F). The user types a search string, and you must jump the cursor to the first place that exact string appears in the document โ€” or tell them it isn't there.

Concretely: given two strings haystack (the text) and needle (the pattern), return the index in haystack where the first occurrence of needle begins, or -1 if needle never appears. This is LeetCode 28, also known as strStr.

The match must be:

  • Contiguous โ€” the characters of needle appear back-to-back, no gaps.
  • In order โ€” "cat" matches "cat", never "tac" or "act".
  • First โ€” if the pattern appears several times, return the smallest starting index.
haystack needle Output Why
"sadbutsad" "sad" 0 "sad" starts at index 0 (also at 6, but 0 wins)
"leetcode" "leeto" -1 "leeto" never appears as a contiguous block
"hello" "ll" 2 "ll" begins at index 2
"abc" "" 0 empty needle matches at the start by convention

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.