Skip to content

Commit 7107aeb

Browse files
authored
Create match-alphanumerical-pattern-in-matrix-i.py
1 parent a66ac73 commit 7107aeb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(n * m * r * c)
2+
# Space: O(1)
3+
4+
# brute force, hash table
5+
class Solution(object):
6+
def findPattern(self, board, pattern):
7+
"""
8+
:type board: List[List[int]]
9+
:type pattern: List[str]
10+
:rtype: List[int]
11+
"""
12+
def check(i, j):
13+
lookup = [-1]*26
14+
lookup2 = [False]*10
15+
for r in xrange(len(pattern)):
16+
for c in xrange(len(pattern[0])):
17+
y = board[i+r][j+c]
18+
if pattern[r][c].isdigit():
19+
if int(pattern[r][c]) != y:
20+
return False
21+
continue
22+
x = ord(pattern[r][c])-ord('a')
23+
if lookup[x] == -1:
24+
if lookup2[y]:
25+
return False
26+
lookup2[y] = True
27+
lookup[x] = y
28+
continue
29+
if lookup[x] != y:
30+
return False
31+
return True
32+
33+
return next(([i, j] for i in xrange(len(board)-len(pattern)+1) for j in xrange(len(board[0])-len(pattern[0])+1) if check(i, j)), [-1, -1])
34+

0 commit comments

Comments
 (0)