-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
check-if-word-can-be-placed-in-crossword.cpp
49 lines (45 loc) · 1.75 KB
/
check-if-word-can-be-placed-in-crossword.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
47
48
49
// Time: O(m * n)
// Space: O(1)
class Solution {
public:
bool placeWordInCrossword(vector<vector<char>>& board, string word) {
for (int d = 0; d < 2; ++d) {
reverse(begin(word), end(word));
for (int transposed = 0; transposed < 2; ++transposed) {
for (int i = 0; i < get_r(board, transposed); ++i) {
auto it = begin(word);
bool matched = true;
for (int j = 0; j < get_c(board, transposed); ++j) {
const char c = get_val(board, i, j, transposed);
if (c == '#') {
if (it == end(word) && matched) {
return true;
}
it = begin(word);
matched = true;
continue;
}
if (!matched) {
continue;
}
matched = it != end(word) && (c == (*it++) || c == ' ');
}
if (it == end(word) && matched) {
return true;
}
}
}
}
return false;
}
private:
int get_r(const vector<vector<char>>& mat, bool transposed) {
return !transposed ? size(mat) : size(mat[0]);
}
int get_c(const vector<vector<char>>& mat, bool transposed) {
return !transposed ? size(mat[0]) : size(mat);
}
int get_val(const vector<vector<char>>& mat, int i, int j, bool transposed) {
return !transposed ? mat[i][j] : mat[j][i];
}
};