Four In A Row Winner
EasyMatrix · Snowflake
A game board is given as a list of equal-length strings board. Each character is either . (an empty cell) or a single uppercase letter, which is a player's token.
A line is four consecutive cells holding the same token, running horizontally, vertically, or along either diagonal.
Return the token that owns a line, or None if there is no line.
If several lines exist, define each line's starting cell as its topmost cell (for a horizontal line, its leftmost cell). Return the token of the line whose starting cell comes first in row-major order (top row first, then left to right within a row).
Example 1
- Input:
- board = [".......", "....X..", "...XO..", "..XOO..", ".XOOOO."]
- Output:
- "X"
- Explanation
Xhas a down-left diagonal from row 1, column 4 to row 4, column 1.Ohas a horizontal line in row 4, but its starting cell is in a later row, soXis returned.
Example 2
- Input:
- board = ["XOXO", "OXOX", "XOXO"]
- Output:
- None
- Explanation
- No row or column has four equal tokens in a row, and with only three rows no diagonal is long enough.
Example 3
- Input:
- board = ["O....", "O....", "O....", "OXXXX"]
- Output:
- "O"
- Explanation
- The vertical
Oline starts at row 0, column 0, which comes before the start of the horizontalXline at row 3, column 1.
Constraints
- 1 ≤
board.length≤ 200 - 1 ≤
board[i].length≤ 200, and all rows have the same length - Each cell is
.or an uppercase English letter