</> 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

String and Pattern

What is this?

These tricky string and grid problems aren't really about scanning substrings โ€” each turns on noticing a derived quantity that makes the brute-force search vanish. The single insight might be the lengths of adjacent runs, one simple counter, a careful flag-based scan, or the offset between two sets of points. Once you summarize the input the right way, the answer is almost already there.

flowchart TD A["A string or grid that looks like a search"] --> B["Compress into runs and compare neighbors"] A --> C["Reduce to one tally or counter"] A --> D["Walk once with flags or an FSM"] A --> E["Tally shift offsets between point sets"]

๐Ÿ’ก Fun fact: For Count Binary Substrings, compressing a string like 00110 into run lengths [2,2,1] means each adjacent pair contributes exactly the smaller of the two runs โ€” turning a substring-counting problem into a quick walk over a handful of numbers.

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


Core idea: Some string and grid problems aren't about substrings at all โ€” they turn on a structural observation: the length of adjacent runs, a single counter, a careful state scan, or the offset between two sets of points. See the structure and the search vanishes.


The pattern

The recurring move is summarize before you compare. Rather than enumerate every substring, compress the string into runs of identical characters and reason about neighboring run lengths โ€” the count of valid groupings depends only on the smaller of two adjacent runs.

Some problems are even simpler than they look: a question dressed up as "balanced halves" reduces to comparing one tally on each side. Others are messy in the opposite direction โ€” validity rules with signs, decimals, and exponents resist clever math, so the honest tool is a careful flag-based scan or finite-state machine that walks the string once and tracks what has legally been seen.

For grids, the insight is work in offset space: instead of overlaying images directly, collect the coordinates of set points and tally the translation vector between every pair; the most frequent shift is the best overlap. The unifying idea: the answer lives in a derived quantity โ€” runs, counts, states, or offsets โ€” not the raw text.


The problems

  • Count Binary Substrings โ€” compress into consecutive run lengths; each adjacent pair contributes min(prev_run, cur_run) balanced substrings.
  • Determine if String Halves Are Alike โ€” just compare the vowel count of the two halves; no rearranging needed.
  • Valid Number โ€” walk the string once with flags (or an FSM) tracking digit, dot, sign, and exponent rules to handle the messy cases.
  • Image Overlap โ€” record the coordinates of 1-cells, tally the translation offset between every cross-pair, and return the most common offset's count.

Key takeaways

  • Summarize into runs โ€” adjacent run lengths answer substring counts.
  • Some "hard" reductions are a tally โ€” count, don't restructure.
  • Messy rules want an FSM โ€” one careful flagged pass, not cleverness.
  • Shift offsets beat overlays โ€” tally translations between point sets.

Image Overlap

Core idea: Don't slide one image over the other and re-count at every offset. Instead, list only the 1-cells of each image, and for every pair (a in img1, b in img2) compute the shift (b.row โˆ’ a.row, b.col โˆ’ a.col) that would land a exactly on top of b. Tally those shifts in a hashmap. Each pair "votes" for one translation; the translation with the most votes is the best overlap, and that vote count is the answer.


1. The problem (LeetCode 835)

You're given two n ร— n binary matrices, img1 and img2 (only 0s and 1s, same size). You may translate one image โ€” slide it left, right, up, or down by any whole number of units โ€” and lay it on top of the other. The overlap of a translation is the number of positions where both images show a 1. Cells that slide off the edge are dropped (no wrap-around).

Return the largest possible overlap over all translations.

A translation is pure sliding โ€” no rotation, no flipping, no scaling.

Constraints: 1 โ‰ค n โ‰ค 30, every entry is 0 or 1.


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.