检查javascript中的2D矩阵中是否存在字符串?

时间:2019-03-25 10:40:41

标签: javascript arrays

我正在尝试在二维数组中找到horizo​​ntalWord字符串。 verticalWord可以正常工作,但是我无法使用horizo​​ntalWord字符串。让我知道您是否有任何想法。

 let matrix = [
    [0, 'r', 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    ['a', 'p', 'p', 'l', 'e'],
    [0, 0, 0, 0, 0]
]

function isInMatrix(matrix, word) {

  for (let j = 0; j < matrix[0].length; j++) {

    let verticalWord = ''
    let horizontalWord = ''

    for (let i = 0; i < matrix.length; i++) {
      verticalWord += matrix[i][j]
    }

    for (let k = 0; k < matrix[0].length; k++) {
              horizontalWord += matrix[j][k]
    }

    if ((verticalWord.includes(word)) ||
      (verticalWord.split('').reverse().join('').includes(word)) ||
      (horizontalWord.includes(word)) ||
      (horizontalWord.split('').reverse().join('').includes(word))) return true
  }
  return false
}

console.log(isInMatrix(matrix, 'apple'))

3 个答案:

答案 0 :(得分:2)

您的循环不正确,您只检查矩阵的前5行

for (let j = 0; j < matrix[0].length; j++) {

使用(let j = 0; j < matrix.length; j++)

答案 1 :(得分:1)

您可以使用嵌套的map

来获得transpose矩阵

然后使用some join 检查内部数组中的includes是否具有给定的word。选中matrix并对其进行转置以进行水平和垂直测试

let input = [
  [0, 'r', 0, 0, 0, 0, 0],
  [0, 'e', 0, 0, 0, 0, 0],
  [0, 'd', 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0],
  [0, 'a', 'p', 'p', 'l', 'e', 0]
]

function isInArray(matrix, word) {
  let transpose = matrix[0].map((a, i) => matrix.map(b => b[i]));

  return matrix.some(a => a.join('').includes(word)) 
        || transpose.some(a => a.join('').includes(word))
}

console.log(isInArray(input, "apple"))
console.log(isInArray(input, "red"))
console.log(isInArray(input, "orange"))

答案 2 :(得分:0)

function isInBoard(board, word) {
    if (word === '') return false;
    if (board.length < 1 || board[0].length < 1) return false;

    const start = findStartingPoint(board, word[0]);
    if (start.length < 1) return false;
    for (let S of start) {
        if (findPathList(board, word.slice(1), S, [])) return true;
    }
    return false;
}

function findPathList(B, W, S, L) {
    L.push(S.join(':'));
    if (!W) return true;
    let r = S[0], c = S[1], rows = B.length, cols = B[0].length;
    const next = [];
    if (r-1 >= 0 && B[r-1][c] === W[0] && !L.includes((r-1)+':'+c)) next.push([r-1, c]);

    if (r+1 < rows && B[r+1][c] === W[0] && !L.includes((r+1)+':'+c)) next.push([r+1, c]);

    if (c-1 >= 0 && B[r][c-1] === W[0] && !L.includes(r+':'+(c-1))) next.push([r, c-1]);

    if (c+1 < cols && B[r][c+1] === W[0] && !L.includes(r+':'+(c+1))) next.push([r, c+1]);

    for (let N of next) {
        const found = findPathList(B, W.slice(1), N, [...L]);
        if (found) {
            return true;
        }
    }
    return false;
}

function findStartingPoint(board, s) {
    const answer = [];
    for (let r = 0; r < board.length; r++) {
        let index = -1;
        while(true) {
            index = board[r].indexOf(s, index+1);
            if (index >= 0) answer.push([r, index]);
            else break;
        }
    }
    return answer;
}

board = [ ['A','B','C','E'],['S','F','C','S'],['A','D','E','E'] ];
words = ["ABCCED", "SEE", "ABCB"];
// true, true, false
for (let word of words) console.log(isInBoard(board, word));
board = [ ["A","B","C","E"],["S","F","E","S"],["A","D","E","E"] ];
word = "ABCESEEEFS";
console.log(isInBoard(board, word));   // true