Easy JavaScript solution (100%) with explanations
In order for the board to be in a valid state we need to check:\n- players did the correct amount of moves (equal amount or player X did one move more)\n- we ha
In order for the board to be in a valid state we need to check:\n- players did the correct amount of moves (equal amount or player `X` did one move more)\n- we have at most one winner (game ends when someone crossed the line). Please notice, that it is possible to cross 2 lines by the last move as well\n- the last move was done by the winner (game ends when someone crossed the line)\n\n```\n/**\n * @param {string[]} board\n * @return {boolean}\n */\nvar validTicTacToe = function(board) {\n  return checkMoves(board) && checkWinner(board);\n};\n\nfunction checkMoves(board) {\n  const [x, o] = countMoves(board);\n  const diff = x - o;\n  return diff === 0 || diff === 1;\n}\n\nfunction countMoves(board) {\n  let x = 0;\n  let o = 0;\n  \n  for (let row of board) {\n    for (let char of row) {\n      if (char === \'X\') {\n        x++;\n        continue;\n      }\n      \n      if (char === \'O\') {\n        o++;\n        continue;\n      }\n    }\n  }\n  \n  return [x, o];\n}\n\nconst winPositions = [\n  [0, 1, 2],\n  [3, 4, 5],\n  [6, 7, 8],\n  \n  [0, 3, 6],\n  [1, 4, 7],\n  [2, 5, 8],\n  \n  [0, 4, 8],\n  [2, 4, 6],\n];\n\nfunction getMove(board, coord) {\n  const row = Math.floor(coord / 3);\n  const col = coord % 3;\n  \n  return board[row][col];\n}\n\nfunction isWinner(board, coords) {\n  const firstMove = getMove(board, coords[0]);\n  \n  if (firstMove === \' \') {\n    return false;\n  }\n  \n  return coords.every(coord => getMove(board, coord) === firstMove);\n}\n\n/**\n * it is possible to have:\n * - no winner\n * - one winner\n * - two winners, but same player\n *\n * Important:\n * Last move should be done by the winner\n *\n * Note: \n * having more than 2 winners is not possible, since it would require\n * more than 5 moves to be done by a player. That is checked by `checkMoves`\n */\nfunction checkWinner(board) {\n  let wins = 0;\n  \n  const winners = winPositions.filter(position => isWinner(board, position));\n  const length = winners.length;\n  \n  // no winner - fine\n  if (length === 0) {\n    return true;\n  }\n  \n  const winnerMove = getMove(board, winners[0][0]);\n  \n  if (!checkLastMove(board, winnerMove)) {\n    return false;\n  }\n  \n  // one winner - all good\n  if (length < 2) {\n    return true;\n  }\n  \n  // two winners. check its same player\n  return winnerMove === getMove(board, winners[1][0]);\n}\n\n// check that the last move was done by the winner\nfunction checkLastMove(board, winnerMove) {\n  const [x, o] = countMoves(board);\n  \n  if (winnerMove === \'X\') {\n    return x > o;\n  }\n  \n  return x === o;\n}\n```
['JavaScript']