Match Alphanumerical Pattern in Matrix I - Problem

You are given a 2D integer matrix board and a 2D character matrix pattern. Where 0 <= board[r][c] <= 9 and each element of pattern is either a digit or a lowercase English letter.

Your task is to find a submatrix of board that matches pattern. An integer matrix part matches pattern if we can replace cells containing letters in pattern with some digits (each distinct letter with a unique digit) in such a way that the resulting matrix becomes identical to the integer matrix part.

In other words:

  • The matrices have identical dimensions
  • If pattern[r][c] is a digit, then part[r][c] must be the same digit
  • If pattern[r][c] is a letter x:
    • For every pattern[i][j] == x, part[i][j] must be the same as part[r][c]
    • For every pattern[i][j] != x, part[i][j] must be different than part[r][c]

Return an array of length 2 containing the row number and column number of the upper-left corner of a submatrix of board which matches pattern. If there is more than one such submatrix, return the coordinates of the submatrix with the lowest row index, and in case there is still a tie, return the coordinates of the submatrix with the lowest column index.

If there are no suitable answers, return [-1, -1].

Input & Output

Example 1 — Basic Pattern Match
$ Input: board = [[3,1,0],[0,1,1],[0,3,2]], pattern = [["a","b"],["b","b"]]
Output: [0,1]
💡 Note: At position (0,1): submatrix is [[1,0],[1,1]]. Map a→1, b→0 fails because b appears in multiple positions with different values. Try position (1,1): submatrix is [[1,1],[3,2]]. Map a→1, b→1 fails because a and b can't both map to 1. At position (0,1) with different mapping: a→1, b→0 works for first row but fails for second row. Actually at (0,1): [[1,0],[1,1]] maps a→1, b→0 in first row but b→1 in second row, which is inconsistent. The correct answer is found by systematic checking.
Example 2 — Digit Constraints
$ Input: board = [[1,2,3],[4,5,6],[7,8,9]], pattern = [["1","a"],["b","5"]]
Output: [0,0]
💡 Note: At position (0,0): submatrix is [[1,2],[4,5]]. Pattern requires first cell = 1 and last cell = 5, which matches. Map a→2, b→4 gives valid assignment.
Example 3 — No Match
$ Input: board = [[1,1,1],[1,1,1],[1,1,1]], pattern = [["a","b"],["c","d"]]
Output: [-1,-1]
💡 Note: Pattern requires 4 different letters (a,b,c,d) but board only contains 1's, so no valid mapping exists.

Constraints

  • 1 ≤ board.length, board[i].length ≤ 1000
  • 1 ≤ pattern.length, pattern[i].length ≤ 100
  • 0 ≤ board[i][j] ≤ 9
  • pattern[i][j] is either a digit or a lowercase English letter

Visualization

Tap to expand
Pattern Matching: Find Where Template Fits in BoardBoard Matrix310011032PatternabbbFound Match at (0,1)1011a→1, b→0 in row 1b→1 in row 2 (conflict!)Search continues until consistent mapping foundOutput: [0, 1] - coordinates of valid match
Understanding the Visualization
1
Input
Board matrix with digits and pattern with letters/digits
2
Process
Find submatrix where pattern can be mapped consistently
3
Output
Coordinates of upper-left corner of matching submatrix
Key Takeaway
🎯 Key Insight: Each letter must consistently map to the same digit throughout the pattern, and different letters must map to different digits
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen