Match Alphanumerical Pattern in Matrix I - Problem

Imagine you're a cryptographer trying to decode a secret pattern hidden within a grid of numbers! πŸ•΅οΈβ€β™‚οΈ

You are given a 2D integer matrix board containing digits (0-9) and a 2D character matrix pattern that serves as your decoder key. The pattern contains both digits and lowercase letters.

Your mission is to find a submatrix within the board that matches the pattern according to these rules:

  • Digit matching: If pattern[r][c] is a digit, then the corresponding cell in the board submatrix must be exactly the same digit
  • Letter constraints: If pattern[r][c] is a letter 'x':
    • All cells in the pattern marked with 'x' must map to the same digit in the board
    • This digit must be different from any digit used by other letters
    • Each letter represents a unique digit (0-9)

Return the coordinates [row, col] of the upper-left corner of the first matching submatrix (prioritizing smaller row, then smaller column). If no match exists, return [-1, -1].

Input & Output

example_1.py β€” Basic Pattern Match
$ Input: board = [[3,1,4],[2,1,3],[0,1,1]], pattern = [['a','1'],['2','a']]
β€Ί Output: [0,0]
πŸ’‘ Note: The submatrix starting at [0,0] is [[3,1],[2,1]]. Pattern 'a' maps to digit 3, '1' stays 1, '2' stays 2. All constraints satisfied!
example_2.py β€” No Match Found
$ Input: board = [[1,2],[3,4]], pattern = [['a','a'],['b','b']]
β€Ί Output: [-1,-1]
πŸ’‘ Note: No 2x2 submatrix exists where two different letters can map to distinct digits consistently.
example_3.py β€” Multiple Letters
$ Input: board = [[1,1,2],[1,2,2],[3,3,3]], pattern = [['a','b']]
β€Ί Output: [0,1]
πŸ’‘ Note: At position [0,1], submatrix is [[1,2]]. Letter 'a' maps to 1, letter 'b' maps to 2. Perfect match!

Constraints

  • 1 ≀ board.length, board[i].length ≀ 50
  • 1 ≀ pattern.length, pattern[i].length ≀ 50
  • 0 ≀ board[r][c] ≀ 9
  • pattern[r][c] is either a digit or a lowercase English letter
  • The pattern dimensions must not exceed board dimensions

Visualization

Tap to expand
Pattern Matching ProcessBoard Matrix (4Γ—6)314221310115Pattern (2Γ—2)a12aValidation ProcessStep 1: Try position [0,0] β†’ Extract submatrix [[3,1],[2,1]]Step 2: Check digits β†’ '1' must equal 1 βœ“, '2' must equal 2 βœ“Step 3: Map letters β†’ 'a' at (0,0) maps to 3Step 4: Validate consistency β†’ 'a' at (1,1) must also be 3, but found 1 βœ—Result: Position [0,0] doesn't matchContinue to next position [0,1]...
Understanding the Visualization
1
Position Template
Place the pattern template at each possible board position
2
Validate Digits
Check that fixed digit pieces match exactly
3
Map Letters
Assign flexible letter pieces to board digits consistently
4
Check Uniqueness
Ensure each letter maps to a unique digit
Key Takeaway
🎯 Key Insight: Success depends on maintaining bidirectional mappings - patternβ†’board for consistency and boardβ†’pattern for uniqueness validation.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
847 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