1958. Check if Move is Legal LeetCode Solution

In this guide, you will get 1958. Check if Move is Legal LeetCode Solution with the best time and space complexity. The solution to Check if Move is Legal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if Move is Legal solution in C++
  4. Check if Move is Legal solution in Java
  5. Check if Move is Legal solution in Python
  6. Additional Resources
1958. Check if Move is Legal LeetCode Solution image

Problem Statement of Check if Move is Legal

You are given a 0-indexed 8 x 8 grid board, where board[r][c] represents the cell (r, c) on a game board. On the board, free cells are represented by ‘.’, white cells are represented by ‘W’, and black cells are represented by ‘B’.
Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only legal if, after changing it, the cell becomes the endpoint of a good line (horizontal, vertical, or diagonal).
A good line is a line of three or more cells (including the endpoints) where the endpoints of the line are one color, and the remaining cells in the middle are the opposite color (no cells in the line are free). You can find examples for good lines in the figure below:

Given two integers rMove and cMove and a character color representing the color you are playing as (white or black), return true if changing cell (rMove, cMove) to color color is a legal move, or false if it is not legal.

See also  667. Beautiful Arrangement II LeetCode Solution

Example 1:

Input: board = [[“.”,”.”,”.”,”B”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”W”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”W”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”W”,”.”,”.”,”.”,”.”],[“W”,”B”,”B”,”.”,”W”,”W”,”W”,”B”],[“.”,”.”,”.”,”B”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”B”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”W”,”.”,”.”,”.”,”.”]], rMove = 4, cMove = 3, color = “B”
Output: true
Explanation: ‘.’, ‘W’, and ‘B’ are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an ‘X’.
The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.

Example 2:

Input: board = [[“.”,”.”,”.”,”.”,”.”,”.”,”.”,”.”],[“.”,”B”,”.”,”.”,”W”,”.”,”.”,”.”],[“.”,”.”,”W”,”.”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”W”,”B”,”.”,”.”,”.”],[“.”,”.”,”.”,”.”,”.”,”.”,”.”,”.”],[“.”,”.”,”.”,”.”,”B”,”W”,”.”,”.”],[“.”,”.”,”.”,”.”,”.”,”.”,”W”,”.”],[“.”,”.”,”.”,”.”,”.”,”.”,”.”,”B”]], rMove = 4, cMove = 4, color = “W”
Output: false
Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.

Constraints:

board.length == board[r].length == 8
0 <= rMove, cMove < 8
board[rMove][cMove] == '.'
color is either 'B' or 'W'.

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

1958. Check if Move is Legal LeetCode Solution in C++

class Solution {
 public:
  bool checkMove(vector<vector<char>>& board, int rMove, int cMove,
                 char color) {
    constexpr int dirs[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
                                {0, 1},   {1, -1}, {1, 0},  {1, 1}};

    for (const auto& [dx, dy] : dirs) {
      int cellsCount = 2;
      int i = rMove + dx;
      int j = cMove + dy;
      while (0 <= i && i < 8 && 0 <= j && j < 8) {
        // There are no free cells in between.
        if (board[i][j] == '.')
          break;
        // Need >= 3 cells.
        if (cellsCount == 2 && board[i][j] == color)
          break;
        // >= 3 cells.
        if (board[i][j] == color)
          return true;
        i += dx;
        j += dy;
        ++cellsCount;
      }
    }

    return false;
  }
};
/* code provided by PROGIEZ */

1958. Check if Move is Legal LeetCode Solution in Java

class Solution {
  public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
    final int[][] dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

    for (int k = 0; k < 8; ++k) {
      final int dx = dir[0][0];
      final int dy = dir[0][1];
      int cellsCount = 2;
      int i = rMove + dx;
      int j = cMove + dy;
      while (0 <= i && i < 8 && 0 <= j && j < 8) {
        // There are no free cells in between.
        if (board[i][j] == '.')
          break;
        // Need >= 3 cells.
        if (cellsCount == 2 && board[i][j] == color)
          break;
        // >= 3 cells.
        if (board[i][j] == color)
          return true;
        i += dx;
        j += dy;
        ++cellsCount;
      }
    }

    return false;
  }
}
// code provided by PROGIEZ

1958. Check if Move is Legal LeetCode Solution in Python

class Solution:
  def checkMove(
      self,
      board: list[list[str]],
      rMove: int,
      cMove: int,
      color: str,
  ) -> bool:
    dirs = ((-1, -1), (-1, 0), (-1, 1), (0, -1),
            (0, 1), (1, -1), (1, 0), (1, 1))

    for dx, dy in dirs:
      cellsCount = 2
      i = rMove + dx
      j = cMove + dy
      while 0 <= i < 8 and 0 <= j < 8:
        # There are no free cells in between.
        if board[i][j] == '.':
          break
        # Need >= 3 cells.
        if cellsCount == 2 and board[i][j] == color:
          break
        # >= 3 cells.
        if board[i][j] == color:
          return True
        i += dx
        j += dy
        cellsCount += 1

    return False
# code by PROGIEZ

Additional Resources

See also  1406. Stone Game III LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.