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

Core idea: a valid substring is a block of k identical bits glued to a block of k of the other bit (0011, 10, โ€ฆ). Every such substring is pinned to the boundary between two adjacent runs โ€” so compress the string into run lengths and, at each boundary, you can form exactly min(left_run, right_run) valid substrings. Sum those minimums.

Problem, rephrased

You're handed a binary string s (only '0' and '1'). Count the non-empty substrings where:

  1. the count of 0s equals the count of 1s, and
  2. all the 0s are grouped consecutively and all the 1s are grouped consecutively.

So "0011" qualifies (two 0s then two 1s), "01" qualifies, but "0101" does not โ€” its 0s and 1s aren't each grouped together. Substrings that occur more than once are counted each time they occur.

Picture the string as a sequence of runs โ€” maximal stretches of one repeated bit. "00110011" is the runs 00 | 11 | 00 | 11, i.e. lengths [2, 2, 2, 2]. Every valid substring straddles exactly one | boundary, taking some bits from the run on the left and an equal number from the run on the right.

s Runs (lengths) Valid substrings Output
"0011" [2, 2] 0011, 01 2
"00110011" [2, 2, 2, 2] 0011, 01, 1100, 10, 0011, 01 6
"10101" [1, 1, 1, 1, 1] 10, 01, 10, 01 4
"000111" [3, 3] 01, 0011, 000111 3
"11110000" [4, 4] 10, 1100, 111000, 11110000 4

Notice row 2: "00110011" itself is not counted โ€” its 0s aren't all grouped together. Only substrings sitting across a single boundary count, and they repeat.

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.