Given a rectangular matrix of English lowercase letters board and a string word, your task is to find the number of occurrences of word in the rows(→), columns(↓) and diagonals(↘) of board.ExampleForboard = [['s', 'o', 's', 'o'],['s', 'o', 'o', 's'],['s', 's', 's', 's']]and word = "sos", the output should be wordCount(board, word) = 3.There are 2 occurrences of word starting from board[0][0](going → and ↘), and one starting from board[0][2](going ↓).No other occurrences of word were counted, so the answer is 3.Forboard = [['a', 'a'],['a', 'a']]and word = "aa", the output should bewordCount(board, word) = 5.There are 2 horizontal, 2, vertical, and 1 diagonal occurrence of word, for a total of 5.
C# Problem Solving #29: Get Word Count
Count number of words in a string js | javascript
3 Tips I’ve learned after 2000 hours of Leetcode
Javascript Algorithm Training Camp - Word Count
const board = [
['s', 'o', 's', 'o'],
['s', 'o', 'o', 's'],
['s', 's', 's', 's']
]
function filterBoard(board, word) {
let matches = 0
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
const insideMatches = {
h: [], // horizontal
v: [], // vertical
d: [] // diagonal
}
for (let letterWord = 0; letterWord < word.length; letterWord++) {
if (board[i] && board[i][j + letterWord]) {
insideMatches.h.push(board[i][j + letterWord]) // run through the array horizontally
}
if (board[i + letterWord] && board[i + letterWord][j]) {
insideMatches.v.push(board[i + letterWord][j]) // run through the array vertically
}
if (board[i + letterWord] && board[i + letterWord][j + letterWord]) {
insideMatches.d.push(board[i + letterWord][j + letterWord]) // run through the array diagonally
}
}
console.log(insideMatches)
Object.keys(insideMatches).forEach(key => {
const matchedWord = insideMatches[key].join('') // join the found letters into a word
if (matchedWord === word) { // check if the formed word matches with the word parameter
matches++ // add into the matches
}
})
}
}
console.log(matches)
}
filterBoard(board, 'sos')