let numRows, numCols;
let dx = [-1, -1, -1, 0, 0, 1, 1, 1];
let dy = [-1, 0, 1, -1, 1, -1, 0, 1];
function
searchWord(grid, r, c, word) {
if
(grid[r] !== word[0])
return
false
;
let wordLength = word.length;
let directionIndex = 0;
while
(directionIndex < 8) {
let k, currentRow = r + dx[directionIndex];
let currentCol = c + dy[directionIndex];
for
(k = 1; k < wordLength; k++) {
if
(currentRow >= numRows || currentRow < 0 ||
currentCol >= numCols || currentCol < 0)
break
;
if
(grid[currentRow][currentCol] !== word[k])
break
;
currentRow += dx[directionIndex];
currentCol += dy[directionIndex];
}
if
(k === wordLength)
return
true
;
directionIndex++;
}
return
false
;
}
function
findPattern(grid, targetWord) {
for
(let r = 0; r < numRows; r++) {
for
(let c = 0; c < numCols; c++) {
if
(searchWord(grid, r, c, targetWord))
console.log(
"Pattern found at "
+ r +
", "
+ c);
}
}
}
numRows = 3;
numCols = 13;
let characterGrid = [
['G
', '
E
', '
E
', '
K
', '
S
', '
F
', '
O
',
'
R
', '
G
', '
E
', '
E
', '
K
', '
S
'],
['
G
', '
E
', '
E
', '
K
', '
S
', '
Q
', '
U
', '
I
', '
Z
',
'
G
', '
E
', '
E
', '
K
'],
['
I
', '
D
', '
E
', '
Q
', '
A
', '
P
', '
R
', '
A
', '
C
',
'
T
', '
I
', '
C
', '
E']
];
findPattern(characterGrid,
"GEEKS"
);
findPattern(characterGrid,
"EEE"
);