Industry code clear and with comments
\nfun validTicTacToe(board: Array<String>): Boolean {\n\n\tfun countChars(c: Char) = board.map{it.filter{it == c}.count()}.sum()  // counts moves per player\n\t
```\nfun validTicTacToe(board: Array<String>): Boolean {\n\n\tfun countChars(c: Char) = board.map{it.filter{it == c}.count()}.sum()  // counts moves per player\n\t\n\tfun countRowWins(c: Char) = board.filter{it.filter{it == c}.count() == 3}.count()\n\tfun countColWins(c: Char) = board[0].indices.map{index -> board.map{it[index]}}.filter{it.filter{it == c}.count() == 3}.count()\n\tfun countDiagWins(c: Char) = board[0].indices.zip(board.indices).filter{board[it.first][it.second] == c}.count()/3\n\tfun countAntiDiagWins(c: Char) = board[0].indices.zip(board.indices.reversed()).filter{board[it.first][it.second] == c}.count()/3   \n\tfun countWins(c: Char) = countRowWins(c)+countColWins(c).also{println(it)}+countDiagWins(c)+countAntiDiagWins(c) // counts the wins for a player\n\n\tval movesX = countChars(\'X\')\n\tval movesO = countChars(\'O\')\n\tval winsX = countWins(\'X\')\n\tval winsO = countWins(\'O\')\n\treturn when {\n\t\tmovesX - movesO > 1 || movesX - movesO < 0 ->  false // player X must be at most 1 move ahead\n\t\twinsX == 0 && winsO == 0 ->  true // noone won, that\'s ok\n\t\twinsX > 0 && winsO == 0 ->  movesX - movesO == 1 // if player X wins, he should have one move more than player O\n\t\twinsX == 0 && winsO > 0 ->  movesX - movesO == 0 // if player O wins, both have an equal amount of moves\n\t\telse -> false // both won\n\t}\n}\n```
[]