LeetCode 1958: Check if Move is Legal Solution

Master LeetCode problem 1958 (Check if Move is Legal), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1958. Check if Move is Legal

Problem Explanation

Explanation:

To solve this problem, we need to check if changing a specific cell to a given color would result in a legal move. A legal move is one where the cell becomes an endpoint of a good line - a line of three or more cells where the endpoints are of one color and the middle cells are of the opposite color.

We can approach this problem by checking in all directions (horizontal, vertical, and diagonal) from the chosen cell to see if there exists a good line.

  1. Start with the chosen cell and check each direction (up, down, left, right, diagonally up-left, up-right, down-left, down-right).
  2. For each direction, count the number of consecutive cells of the opposite color until we reach a cell of the same color or the board boundary.
  3. If the count is 2 or more, and the next cell in that direction is of the same color, then it is a legal move.

Complexity Analysis:

  • Time Complexity: O(1) because we are exploring a fixed number of directions from the chosen cell.
  • Space Complexity: O(1) as we are using a constant amount of extra space.

:

Solution Code

class Solution {
    public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
        int[] dx = {0, 0, 1, -1, 1, -1, 1, -1};
        int[] dy = {1, -1, 0, 0, 1, 1, -1, -1};
        
        for (int i = 0; i < 8; i++) {
            int x = rMove + dx[i];
            int y = cMove + dy[i];
            int count = 0;
            
            while (x >= 0 && x < 8 && y >= 0 && y < 8 && board[x][y] != '.') {
                if (board[x][y] != color) {
                    count++;
                } else if (count >= 2) {
                    return true;
                } else {
                    break;
                }
                x += dx[i];
                y += dy[i];
            }
        }
        
        return false;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1958 (Check if Move is Legal)?

This page provides optimized solutions for LeetCode problem 1958 (Check if Move is Legal) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1958 (Check if Move is Legal)?

The time complexity for LeetCode 1958 (Check if Move is Legal) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1958 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1958 in Java, C++, or Python.

Back to LeetCode Solutions