Java union find beats 99.7%. Easy to understand and with explanation.
The idea: We use each cell corner as nodes in a graph. For example, the following square of 2x2:\n \n|__ |__ |\n|__ |__ |\n\n\nBecomes the grid:\n\n.  .  .\n.  
The idea: We use each cell corner as nodes in a graph. For example, the following square of 2x2:\n ```\n|__ |__ |\n|__ |__ |\n```\n\nBecomes the grid:\n```\n.  .  .\n.  .  .\n.  .  .\n```\nThat is a grid of dimension (2+1)x(2+1). So the formula for building it is using (N+1)x(N+1).\nIf we are at cell i,j of the original square, the top-left node for that cell on the grid is i,j, the top-right is i,j+1, the bottom left is i+1,j and the bottom-right is i+1,j+1.\n\nNow we use disjoint sets on that grid of nodes. We start by connecting the nodes of the borders. For that we do union on all the nodes of the border.  We then traverse the input. For every \'\\\\\' we find on a cell i,j of the original square, we connect the top-left with the bottom-right nodes of the grid, for every \'/\' we connect the top-right node of the grid with the bottom-left node.\n\nWhenever we are connecting two nodes that are already connected to each other, we will inevitably close a region, add +1 region to our answer. Otherwise, no region will be closed by the new connection, so just keep going.\n\n```\nclass Solution {\n    public int regionsBySlashes(String[] grid) {\n        int N = grid.length;\n        Node[][] nodes = new Node[N+1][N+1];\n        nodes[0][0] = new Node();\n        \n        for(int i = 0; i < nodes.length; i++) {\n            for(int j = 0; j < nodes[i].length; j++) {\n                if(nodes[i][j] == null) {\n                    nodes[i][j] = new Node();\n                }\n            }\n        }\n        \n        //setting parent of the border\n        fillLine(nodes,0,nodes[0][0]);\n        fillLine(nodes,nodes.length-1,nodes[0][0]);\n        fillColumn(nodes,0,nodes[0][0]);\n        fillColumn(nodes,nodes[0].length-1,nodes[0][0]);\n        \n        \n        int ans = 1;\n        \n        for(int i =0; i < grid.length; i++) {\n            for(int j = 0; j < grid[i].length(); j++) {\n                char symbol = grid[i].charAt(j);\n                if(symbol == \'\\\\\') {\n                    if(find(nodes[i][j]) == find(nodes[i+1][j+1])) {\n                        ans++;\n                    }\n                    union(nodes[i][j],nodes[i+1][j+1]);\n                } else if (symbol == \'/\') {\n                    if (find(nodes[i][j+1]) == find(nodes[i+1][j])) {\n                        ans++;\n                    }\n                    union(nodes[i][j+1],nodes[i+1][j]);\n                }\n            }\n        }\n        \n        return ans;\n    }\n    //find set\n    private Node find(Node n) {\n        Node parent = n.parent;\n        if (n.parent != n) {\n            parent = find(n.parent);\n        }\n        n.parent = parent;//path compression\n        return parent;\n    }\n    //union without ranking\n    private void union(Node a, Node b) {\n        Node pA = find(a);\n        Node pB = find(b);\n        pA.parent = pB;\n    }\n    private void fillLine(Node[][] nodes, int line, Node parent) {\n        for(int j = 0; j < nodes[line].length; j++) {\n            if(nodes[line][j] == null) {\n                nodes[line][j] = new Node();\n            }\n            nodes[line][j].parent = parent;\n        }\n    }\n    private void fillColumn(Node[][] nodes, int column, Node parent) {\n        for(int i = 0; i < nodes.length; i++) {\n            if(nodes[i][column] == null) {\n                nodes[i][column] = new Node();\n            }\n            nodes[i][column].parent = parent;\n        }\n    }\n}\nclass Node {\n    public Node parent;\n    public Node () {\n        this.parent = this;\n    }\n}\n```
[]