Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Check if Move is Legal

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1958" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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. 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'.

Explanation

Here's a breakdown of the solution:

  • Check all 8 directions: For the given cell (rMove, cMove), iterate through all 8 possible directions (horizontal, vertical, and diagonals).
  • Search for a good line in each direction: In each direction, check if placing the given color at (rMove, cMove) creates a "good line". This involves traversing the line from (rMove, cMove) outwards and verifying that the adjacent cells follow the good line pattern (opposite color followed by same color as the endpoint).
  • Return true if any good line is found: If a good line is found in any of the 8 directions, return true. Otherwise, return false.

  • Runtime Complexity: O(1). Since the board size is fixed at 8x8, and the search in each direction has a limited scope, the overall complexity is constant.

  • Storage Complexity: O(1). The algorithm uses only a few variables and doesn't depend on the input size.

Code

    def checkMove(board, rMove, cMove, color):
    def isValid(r, c):
        return 0 <= r < 8 and 0 <= c < 8

    def checkDirection(dr, dc):
        oppositeColor = 'W' if color == 'B' else 'B'
        count = 0
        r, c = rMove + dr, cMove + dc
        while isValid(r, c) and board[r][c] == oppositeColor:
            count += 1
            r += dr
            c += dc

        if count == 0:
            return False

        if isValid(r, c) and board[r][c] == color:
            return True

        return False

    directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
    for dr, dc in directions:
        if checkDirection(dr, dc):
            return True

    return False

More from this blog

C

Chatmagic blog

2894 posts