Count Islands w/ DFS (Java)
This can be transformed to a simple island counting problem by first converting each input to a 3 x 3 grid. We simply convert slashes in the following format:\n
This can be transformed to a simple island counting problem by first converting each input to a 3 x 3 grid. We simply convert slashes in the following format:\n\n```\n\'/\':      \'\\\\\':      \' \':\n0 0 1      1 0 0     0 0 0\n0 1 0      0 1 0     0 0 0\n1 0 0      0 0 1     0 0 0\n```\n\nthen fill the border of our grid w/ 1s, and simply count islands of 0s to determine the number of regions.\n\n```\nclass Solution {\n    \n    /**\n    \n    Example mapping of input: ["/"] to 1s grid.\n    \n    1 1 1 1 1\n    1 0 0 1 1\n    1 0 1 0 1\n    1 1 0 0 1\n    1 1 1 1 1\n    \n    */\n    \n    public boolean[][] visited;\n    \n    public int regionsBySlashes(String[] grid) {\n        \n        if (grid == null || grid.length == 0) return 0;\n        \n        int dim = grid.length * 3;\n        \n        // 2 additional rows / columns to account for border\n        \n        int[][] gridIslands = new int[dim + 2][dim + 2];\n        visited = new boolean[dim + 2][dim + 2];\n        \n        for (int i = 0; i < gridIslands.length; i++) {\n            gridIslands[0][i] = 1;\n            gridIslands[gridIslands.length - 1][i] = 1;\n            gridIslands[i][0] = 1;\n            gridIslands[i][gridIslands.length - 1] = 1;\n        }\n        \n        // convert slashes to 1s\n        for (int i = 0; i < grid.length; i++) {\n            String slashes = grid[i];\n            for (int j = 0; j < slashes.length(); j++) {\n                int starti = (i + 1) * 3 - 2;\n                int startj = (j + 1) * 3 - 2;\n                if (slashes.charAt(j) == \'\\\\\') {\n                    for (int m = starti, n = 0; m < starti + 3; m++, n++) {\n                        gridIslands[m][n + startj] = 1;\n                    }\n                } else if (slashes.charAt(j) == \'/\') {\n                    for (int m = starti, n = 2; m < starti + 3; m++, n--) {\n                        gridIslands[m][n + startj] = 1;\n                    }\n                }\n            }\n        }\n    \n        // count islands\n        int count = 0;\n        for (int i = 0; i < gridIslands.length; i++) {\n            for (int j = 0; j < gridIslands[0].length; j++) {\n                if (gridIslands[i][j] == 0 && !visited[i][j]) {\n                    dfs(i, j, gridIslands);\n                    count++;\n                }\n            }\n        } \n            \n        return count;\n    }\n    \n    private void dfs(int i, int j, int[][] gridIslands) {\n        if (i >= 0 && j >= 0 && i < gridIslands.length && j < gridIslands[0].length && gridIslands[i][j] == 0 && !visited[i][j]) {\n            visited[i][j] = true;\n            dfs(i + 1, j, gridIslands);\n            dfs(i - 1, j, gridIslands);\n            dfs(i, j + 1, gridIslands);\n            dfs(i, j - 1, gridIslands);\n        }\n    }\n}\n```
['Depth-First Search']