1131. Line of K Marks

Easy1.5 points estimatedMatrixEnumerationSimulation

Two players fill an m x n board with marks. The grid board contains 'X', 'O' and '.' for an empty square. A player wins by owning k consecutive equal marks along a row, a column, a diagonal or an anti-diagonal.

Scan every line of the board and return 'X' or 'O' for the player who owns such a line, or '.' when neither player has one. The inputs guarantee that at most one player owns a line of length k, so the answer is never ambiguous.

Constraints: 1 <= m, n <= 20 and 1 <= k <= max(m, n). Trying every cell as the start of a line in four directions takes O(m*n*k) time and O(1) extra space.

Example 1

Input:
board = [["X","O","X"],["O","X","O"],[".",".","X"]]k = 3
Output:
"X"
Explanation:

The main diagonal holds X three times in a row, so X wins.

Example 2

Input:
board = [["X","X","O"],["O","O","X"],["X","O","X"]]k = 3
Output:
"."
Explanation:

No row, column or diagonal of length 3 is filled with one mark, so the result is a dot.

Example 3

Input:
board = [["O","O","O","O"],["X",".","X","."],[".",".","X","."]]k = 4
Output:
"O"
Explanation:

The first row contains four O marks in a row, so O wins.

Constraints

1 ≤ m, n ≤ 20

1 ≤ k ≤ max(m, n)

at most one player owns a k-line

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(m*n*k)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…