Java UnionFind without Optimization
\npublic static int regionsBySlashes(String[] grid) {\n        int n = grid.length;\n\n        int[] parents = new int[n * n * 4];\n        int parent = 0;\n\n 
```\npublic static int regionsBySlashes(String[] grid) {\n        int n = grid.length;\n\n        int[] parents = new int[n * n * 4];\n        int parent = 0;\n\n        int col;\n        char ch;\n        for (int row = 0; row < n; row++) {\n            col = 0;\n            while (col < grid[row].length()) {\n                ch = grid[row].charAt(col);\n                if (ch == \'/\') {\n                    parents[parent] = parent;\n                    parents[parent + 1] = parent + 1;\n                    parents[parent + 2] = parent + 1;\n                    parents[parent + 3] = parent;\n                } else if (ch == \'\\\\\') {\n                    parents[parent] = parent;\n                    parents[parent + 1] = parent;\n                    parents[parent + 2] = parent + 2;\n                    parents[parent + 3] = parent + 2;\n                } else {\n                    parents[parent] = parent;\n                    parents[parent + 1] = parent;\n                    parents[parent + 2] = parent;\n                    parents[parent + 3] = parent;\n                }\n                parent += 4;\n\n                if (col > 0) {\n                    // union with left\n                    union(parents, row * 4 * n + (col - 1) * 4 + 1, row * 4 * n + (col) * 4 + 3);\n                }\n                if (row > 0) {\n                    // union with top\n                    union(parents, row * 4 * n + col * 4, (row - 1) * 4 * n + (col) * 4 + 2);\n                }\n\n                col++;\n            }\n\n        }\n\n        int counter = 0;\n        for (int i = 0; i < parents.length; i++) {\n            if (parents[i] == i)\n                counter++;\n        }\n\n        return counter;\n    }\n\n\n    public static void union(int[] parents, int i, int j) {\n        int root1 = root(parents, i);\n        int root2 = root(parents, j);\n        if (root1 == root2)\n            return;\n        // TODO to balance?\n        parents[root1] = root2;\n    }\n\n\n    private static int root(int[] parents, int i) {\n        while (i != parents[i])\n            i = parents[i];\n        return i;\n    }\n```
[]