Weather Burst Reassembly

Find all starts matching a multiset of equal-length strings

HardArrays & HashingSliding WindowFrequency Counting

A weather station sends diagnostic codes without separators. Its receiver has recorded the resulting character string stream.

A scheduled diagnostic burst must contain every occurrence in the list codes, in any order, with no other characters between them. All codes have the same positive length. Repeated entries in codes represent separate required transmissions.

Return all zero-based starting indices, in increasing order, where a contiguous part of stream is exactly one complete diagnostic burst. Matches may overlap, and a match may begin at any character position, not just a multiple of the code length.

Characters outside a matching burst do not matter.

Examples

Example 1

Input: stream = "zzabcdababcdyy", codes = ["ab","cd","ab"]
Output: [2,4,6]

The required burst contains two copies of `ab` and one copy of `cd`. The stream contains overlapping arrangements of these three transmissions; the surrounding `zz` and `yy` are not part of a burst.

Example 2

Input: stream = "aaaa", codes = ["a","a"]
Output: [0,1,2]

Each burst needs two separate copies of `a`. Overlapping pairs of characters are allowed to form different matches.

Example 3

Input: stream = "rain", codes = ["rain","mist"]
Output: []

The complete required burst is longer than the recorded stream, so no starting position can match.

Constraints

  • 0 <= len(stream) <= 100000
  • 1 <= len(codes) <= 5000
  • All entries in codes have the same length w, where 1 <= w <= 20
  • stream and every code contain only lowercase English letters

Let n be the stream length, k the number of codes, and w the common code length. With ordinary string slicing and hashing, the intended solution takes expected O((n + k) * w) time and O(n + k * w) auxiliary space, including a marker array used to return indices in increasing order.

Hints

Show hint 1

For a fixed starting position modulo the code length, the stream becomes a sequence of nonoverlapping, fixed-width tokens.

Show hint 2

Within each alignment, maintain token frequencies. An unknown token breaks a candidate burst; an excessive frequency can be repaired by advancing the left boundary.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you report matches incrementally if the stream arrived in chunks that could split a code?
  • How would the approach change if codes could have different lengths?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem