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
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.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code