LeetCode 959: Regions Cut By Slashes Solution

Master LeetCode problem 959 (Regions Cut By Slashes), 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.

Problem Explanation

Explanation:

To solve this problem, we can treat each grid cell as divided into 4 sub-cells by the slashes ('/', '', ' '). Then, we can perform a depth-first search (DFS) to identify the regions formed by these sub-cells. We will mark visited sub-cells and count the number of regions as we traverse the grid.

  1. For each cell in the grid:

    • Divide the cell into 4 sub-cells (top-left, top-right, bottom-left, bottom-right).
    • Mark visited sub-cells as we traverse the grid.
    • Perform DFS to identify regions within each cell.
  2. In the DFS:

    • Start from an unvisited sub-cell and explore all neighboring unvisited sub-cells.
    • Mark visited sub-cells and increment the region count.
    • Continue until all sub-cells are visited.
  3. Return the total number of regions found in the grid.

Time Complexity: O(n^2) where n is the size of the grid. Space Complexity: O(n^2) for storing visited information and recursion stack.

:

Solution Code

class Solution {
    public int regionsBySlashes(String[] grid) {
        int n = grid.length;
        int[][] visited = new int[3 * n][3 * n];
        int regions = 0;
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i].charAt(j) == '/') {
                    mark(visited, i * 3, j * 3 + 2);
                    mark(visited, i * 3 + 1, j * 3 + 1);
                    mark(visited, i * 3 + 2, j * 3);
                } else if (grid[i].charAt(j) == '\\') {
                    mark(visited, i * 3, j * 3);
                    mark(visited, i * 3 + 1, j * 3 + 1);
                    mark(visited, i * 3 + 2, j * 3 + 2);
                }
            }
        }
        
        for (int i = 0; i < 3 * n; i++) {
            for (int j = 0; j < 3 * n; j++) {
                if (visited[i][j] == 0) {
                    regions++;
                    dfs(visited, i, j);
                }
            }
        }
        
        return regions;
    }
    
    private void mark(int[][] visited, int i, int j) {
        if (i >= 0 && i < visited.length && j >= 0 && j < visited[0].length) {
            visited[i][j] = 1;
        }
    }
    
    private void dfs(int[][] visited, int i, int j) {
        if (i >= 0 && i < visited.length && j >= 0 && j < visited[0].length && visited[i][j] == 0) {
            visited[i][j] = 1;
            int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            for (int[] dir : dirs) {
                int ni = i + dir[0];
                int nj = j + dir[1];
                dfs(visited, ni, nj);
            }
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 959 (Regions Cut By Slashes)?

This page provides optimized solutions for LeetCode problem 959 (Regions Cut By Slashes) 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 959 (Regions Cut By Slashes)?

The time complexity for LeetCode 959 (Regions Cut By Slashes) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 959 on DevExCode?

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

Back to LeetCode Solutions