-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
match-alphanumerical-pattern-in-matrix-i.py
34 lines (31 loc) · 1.18 KB
/
match-alphanumerical-pattern-in-matrix-i.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Time: O(n * m * r * c)
# Space: O(1)
# brute force, hash table
class Solution(object):
def findPattern(self, board, pattern):
"""
:type board: List[List[int]]
:type pattern: List[str]
:rtype: List[int]
"""
def check(i, j):
lookup = [-1]*26
lookup2 = [False]*10
for r in xrange(len(pattern)):
for c in xrange(len(pattern[0])):
y = board[i+r][j+c]
if pattern[r][c].isdigit():
if int(pattern[r][c]) != y:
return False
continue
x = ord(pattern[r][c])-ord('a')
if lookup[x] == -1:
if lookup2[y]:
return False
lookup2[y] = True
lookup[x] = y
continue
if lookup[x] != y:
return False
return True
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])