Skip to main content

Connect Four Move Validator

easy
Software engineer

You are given a 2D array board representing a game grid where each cell contains either "a", "b", or an empty string "".

A player (either "a" or "b") makes a move by placing their piece at the specified coordinates [x, y]. Your task is to determine if this specific move completes a line of four consecutive pieces of that player's type in any of the following directions:

  • Horizontal (row)
  • Vertical (column)
  • Diagonal (top-left to bottom-right)
  • Anti-diagonal (top-right to bottom-left)

If the move results in a win, return the player's name ("a" or "b"). Otherwise, return an empty string "".

It is guaranteed that there was no winner before this move, and the target cell [x, y] is currently empty and within bounds.

Example 1

Input

board = [["a", "", "b", ""], ["", "a", "b", ""], ["", "b", "a", ""], ["b", "", "", ""]], move = [3, 3], player = "a"

Output

"a"

Explanation

Placing "a" at (3, 3) completes a diagonal of four "a"s starting from (0, 0) through (1, 1), (2, 2), and finally (3, 3). Since a line of four is formed, the player's name "a" is returned.

Example 2

Input

board = [["a","","b",""], ["","a","b",""], ["","a","b",""], ["b","","",""]], move = [3, 3], player = "a"

Output

""

Explanation

Placing "a" at (3, 3) does not result in four consecutive "a"s in any horizontal, vertical, or diagonal direction.

Example 3

Input

board = [["a","b","a","","","",""], ["","b","b","b","","",""], ["","a","b","a","","",""], ["","b","a","b","","",""], ["","a","b","a","","",""], ["","b","a","b","","",""], ["","a","b","a","","",""]], move = [1, 4], player = "b"

Output

"b"

Explanation

Placing "b" at (1, 4) completes a horizontal line of four "b"s in row 1: (1, 1), (1, 2), (1, 3), and (1, 4).

Constraints

  • 1 <= board.length, board[i].length <= 100
  • move.length == 2
  • board[x][y] == ""
  • player is either "a" or "b"

Screening

ArrayEnumerationMatrixSimulation
Language
Code editor loads in the browser.

Output

Input

[["a","","b",""],["","a","b",""],["","b","a",""],["b","","",""]]
[3,3]
"a"

Expected

"a"

Your output

Run to see your output.