Well-Explained || Union Find Method || C++
\n\n# Code\n\nclass Solution {\npublic:\n    vector<int>parent;\n\n    int find(int x){\n        if(parent[x]==x)return x;\n        else\n        return find(pa
\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int>parent;\n\n    int find(int x){\n        if(parent[x]==x)return x;\n        else\n        return find(parent[x]);\n    }\n\n    int regionsBySlashes(vector<string>& grid) {\n        //Idea is we see that if / are connected till the boundary of grid means it has divied , to check that we see the parent of starting index of / & ending index of / if same means they are diving, same goes for \\ case\n\n        //for seeing the bounary all points on boundary have parent=0;\n        //for nxn matrix we will make n+1 X n+1 matrix so we can get inner points for / & \\ \n        // (0,0)---(0,1)---(0,2)\n        //   |        |   /    |\n        //  (1,0)---(1,1)---(1,2)\n        //   |    \\   |        |\n        //  (2,0)---(2,1)----(2,2)\n        //for this example for maxrix 2x2 we will make 3x3 and whenever we have / we\'ll check\n        //parent (0,1) should be same as (1,1)for cycle\n        //for \\ parent (1,0) should be same as parent (2,1)\n\n        //for finding parent we have union & find methods of disjoint set.\n\n\n        vector<vector<int>>mat(grid.size()+1,vector<int>(grid.size()+1));\n        int n=mat.size();\n        parent.resize(n*n);\n\n        for(int i=0;i<n;i++){\n            for(int j=0;j<n;j++){\n                if(i==0||j==0 || i==n-1 || j==n-1){\n                    parent[i*n+j]=0;//index of i,j in serialized parent array\n                }else{\n                    parent[i*n+j]=(i*n)+j;\n                }\n            }\n        }\n        int res=1;\n        for(int i=0;i<grid.size();i++){\n            for(int j=0;j<grid.size();j++){\n                if(grid[i][j]==\'/\'){\n                    int par1=find(i*n+(j+1));\n                    int par2=find((i+1)*n+j);\n                    if(par1==par2)res++;\n                    else{\n                        parent[par1]=par2;\n                    }\n                }\n                else if(grid[i][j]==\'\\\\\'){\n                    int par1=find(i*n+j);\n                    int par2=find((i+1)*n+(j+1));\n                    if(par1==par2)res++;\n                    else{\n                        parent[par1]=par2;\n                    }\n                }\n            }\n        }\n        return res;\n    }\n};\n\n```
['Union Find', 'C++']