-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcheck-if-word-can-be-placed-in-crossword.cc
More file actions
37 lines (35 loc) · 1.08 KB
/
check-if-word-can-be-placed-in-crossword.cc
File metadata and controls
37 lines (35 loc) · 1.08 KB
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
// Check if Word Can Be Placed In Crossword
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
class Solution {
public:
bool placeWordInCrossword(vector<vector<char>>& g, string word) {
int n = g.size(), m = g[0].size();
REP(_, 2) {
REP(i, n)
REP(j, m)
if (g[i][j] != '#') {
int jj = j+1;
while (jj < m && g[i][jj] != '#') jj++;
if (jj-j == word.size()) {
int k = 0;
for (; k < word.size() && (g[i][j+k] == ' ' || word[k] == g[i][j+k]); k++);
if (k == word.size())
return true;
k = 0;
for (; k < word.size() && (g[i][j+k] == ' ' || word[word.size()-1-k] == g[i][j+k]); k++);
if (k == word.size())
return true;
}
j = jj-1;
}
vector<vector<char>> h(m, vector<char>(n));
REP(i, n)
REP(j, m)
h[j][i] = g[i][j];
g.swap(h);
swap(n, m);
}
return false;
}
};