-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
match-alphanumerical-pattern-in-matrix-i.cpp
46 lines (44 loc) · 1.48 KB
/
match-alphanumerical-pattern-in-matrix-i.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
// Time: O(n * m * r * c)
// Space: O(1)
// brute force, hash table
class Solution {
public:
vector<int> findPattern(vector<vector<int>>& board, vector<string>& pattern) {
const auto& check = [&](int i, int j) {
vector<int> lookup(26, -1);
vector<bool> lookup2(10);
for (int r = 0; r < size(pattern); ++r) {
for (int c = 0; c < size(pattern[0]); ++c) {
const int y = board[i + r][j + c];
if (isdigit(pattern[r][c])) {
if (pattern[r][c] - '0' != y) {
return false;
}
continue;
}
const int x = pattern[r][c] - '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;
};
for (int i = 0; i + size(pattern) - 1 < size(board); ++i) {
for (int j = 0; j + size(pattern[0]) - 1 < size(board[0]); ++j) {
if (check(i, j)) {
return {i, j};
}
}
}
return {-1, -1};
}
};