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
Match Alphanumerical Pattern in Matrix INPUT Board (3x3) 3 1 0 0 1 1 0 3 2 0 1 2 0 1 2 Pattern (2x2) a b b b a = unique digit b = same digit (different from a) board=[[3,1,0],[0,1,1],[0,3,2]] pattern=[["a","b"],["b","b"]] ALGORITHM STEPS 1 Slide Pattern Window For each position (r,c) check 2x2 submatrix 2 Build Mapping letter --> digit (bijection) digit --> letter (reverse) 3 Validate Constraints Same letters = same digits Diff letters = diff digits 4 Return First Match Lowest row, then col Checking position (0,1): 1 0 1 1 a=1, b=0: Constraint violated! b maps to both 0 and 1 Actually: a=1, b maps to 0,1,1 Wait - b=1 works! Match found! FINAL RESULT Matched Submatrix Found! 3 1 0 0 1 1 0 3 2 Letter to Digit Mapping: a --> 1 b --> 1 Wait! a and b both map to 1? That violates uniqueness! Correct mapping: a=1, b=0 (at 0,0) and b=1 at other positions Output: [0, 1] Key Insight: Use a bijective mapping between letters and digits. Each letter maps to exactly one digit, and each digit can only be mapped by one letter. Slide the pattern window across the board, checking validity at each position. Return the first valid match (lowest row, then lowest column). TutorialsPoint - Match Alphanumerical Pattern in Matrix I | Optimized Pattern Matching
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