Stream Mosaic Cue Index

Indices of strings realizable along nonrepeating grid paths

HardBacktrackingTriesGrid Search

A music-streaming app displays a rectangular mosaic of lowercase letter tiles. Each promotional cue has a code in codes.

A code can be traced by starting at any tile and moving between tiles that share a side: up, down, left, or right. The letters visited, in order, must spell the entire code. A tile cannot be visited twice while tracing one code.

Different codes are checked independently, so they may use the same tiles. A one-letter code requires only a matching starting tile.

Return the zero-based indices of all codes that can be traced, in increasing order. Include each qualifying index exactly once, even if its code has several possible traces.

Examples

Example 1

Input: tiles = [["a","b"],["c","d"]], codes = ["abd","acdb","aba","ad","b"]
Output: [0,1,4]

The code "abd" follows the top-left, top-right, and bottom-right tiles. The code "acdb" follows all four tiles around the boundary. "aba" would require revisiting the only 'a' tile, and "ad" would require a diagonal move. The one-letter code "b" needs no move.

Example 2

Input: tiles = [["m","m","n"]], codes = ["mm","mmm","mnm","nmm"]
Output: [0,3]

There are two adjacent 'm' tiles, so "mm" can be traced. There are not three distinct 'm' tiles for "mmm". Starting at the rightmost tile traces "nmm", but "mnm" cannot be traced without revisiting a tile or skipping over one.

Constraints

  • 1 <= number of rows in tiles <= 10
  • 1 <= number of columns in tiles <= 10
  • tiles is rectangular and every cell is a single lowercase English letter
  • 1 <= codes.length <= 4000
  • 1 <= codes[i].length <= 10
  • Every code contains only lowercase English letters
  • All codes are distinct
  • The sum of all code lengths is at most 40000

A separate grid search for every code repeats work on shared prefixes. The reference solution instead searches a trie and permanently removes exhausted branches. Let S be the total code length, B the number of tiles, and L the maximum code length. A conservative worst-case time bound is O(S + B * 4 * 3^(L - 1)); prefix checks and branch removal often reduce the actual search substantially. Auxiliary space is O(S + B + codes.length + L).

Hints

Show hint 1

Many codes may share a prefix. Can their searches share the work of tracing that prefix?

Show hint 2

Store the codes in a trie. During backtracking, report and remove a reached terminal, then remove branches that no longer contain any unreported code.

Follow-up questions

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

  • How would you return the lexicographically smallest coordinate sequence for each traceable code?
  • How would the search change if each tile could be visited at most twice within a trace?

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