KatInterview

Locate Symbol-Structure Templates in a Number Grid

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashinghash-mappattern-matchingmatrix

Last updated

Tests whether a candidate can slide a letter-template over an integer grid and detect every position where symbols and values form a valid one-to-one correspondence.

Locate Symbol-Structure Templates in a Number Grid

You are given a rectangular grid of integers and a small rectangular template made of uppercase letters. Your task is to find every position in the grid where the template "fits" — meaning the relationship between letters in the template mirrors the relationship between numbers in the corresponding grid window.

What "fits" means

For a given top-left corner (r, c), extract the subgrid of the same height and width as the template. The template fits at (r, c) if and only if there is a one-to-one mapping between the letters in the template and the integers in the subgrid such that:

  • Wherever the same letter appears in the template, the exact same integer appears in the subgrid at those positions.
  • Wherever two different letters appear, they must correspond to two different integers.

This mapping is local to each candidate window — a letter may map to one integer in one window and a completely different integer in another.

Return a list of [row, col] pairs for every window where the template fits, sorted first by row and then by column.

Constraints

  • The grid has between 1 and 50 rows and between 1 and 50 columns.
  • The template has between 1 and 5 rows and between 1 and 5 columns.
  • Template cells contain only uppercase ASCII letters (A–Z).
  • Grid values are integers in the range [-1,000,000,000, 1,000,000,000].
  • The template must fit entirely within the grid; no wrapping or partial overlap is allowed.
  • If the template is taller or wider than the grid, return an empty list.
  • Do not rotate, reflect, scale, or otherwise transform the template.

Examples

Example 1

Input: locate_template_in_grid([[1, 2, 4, 5], [2, 3, 6, 7], [6, 4, 2, 2]], ['AT', 'TB'])

Output: [[0, 0]]

Example 2

Input: locate_template_in_grid([[7, 7], [7, 7]], ['AA', 'AA'])

Output: [[0, 0]]

Hints

Hint 1

Start by thinking about what makes two grids 'structurally identical': the pattern of equal and unequal values must be the same. This is the same idea as checking whether two strings are isomorphic, extended to two dimensions.

Hint 2

A single forward map (letter → integer) is not enough. Consider the template 'AB' placed over the window [5, 5]: A maps to 5 and B maps to 5 without any forward-map conflict, yet A and B are different letters and must map to different integers. You need a second reverse map (integer → letter) to catch this case.

Hint 3

For each candidate window, build both maps from scratch. Scan cells in row-major order. At each cell, check both maps before writing to them: if a letter already has a different integer, or an integer already belongs to a different letter, reject the window immediately and move to the next.