Java || Graph || Union Find || With Comments
\n // b <============= 959. Regions Cut By Slashes ================>\n    // https://leetcode.com/problems/regions-cut-by-slashes/\n\n    // # Bfs and DSU can f
```\n // b <============= 959. Regions Cut By Slashes ================>\n    // https://leetcode.com/problems/regions-cut-by-slashes/\n\n    // # Bfs and DSU can find number of distinct cycles, not the overlapping one.\n\n    // ! If I get two points, then only I can create a line .\n    // ! So Basically I would be needing two points.\n    // ! Ek line banane ke liye do points to chahiye he honge.\n\n    // # So Agar tu number of distincts cycles nikal de, to tereko wo number of\n    // # regions mil jayenge jo slashes ne cut kiya hoga\n\n    // ? Per ab isko karein kaise ????\n\n    // Kuch aise grid de rakhi hai grid = ["/\\\\","\\\\/"]\n    // Now consider this grid as a 2-d matrix. With string as an array stroring some\n    // values in 0th and 1st index.\n\n    // Ab forward slash and backward slash kuch region cut kar rahe hain.\n    // let n=grid.length\n    // ` Unko show karne ke liye ek +1 size ki matrix ki jaroorat hogi\n\n    // Let N=n+1\n    // To humne N*N ki matrix consider ki aur usme points mark kiye.\n\n    // Agar grid mai (0,0) mai / hai to wo (x,y+1) and (x+1,y) points pe draw hoga\n    // Agar grid mai (0,1) mai \\\\ hai to wo (x,y) and (x+1,y+1) points pe draw hoga\n\n    // Ab bas same union find algo lagaya aur grid se ek ek edge nikalte rahe.\n    // Aur number of cycles count kar li.\n\n    // # Initially cycle ka count 1 hoga kyunki boundary bhi to ek cycle bana rahi\n    // # hogi\n\n    // Ab kyunki boundary ek cycle hai to aur sare edges ek dusre se connected hain\n    // to unka parent ek he hoga initially.\n\n    static int[] par;\n    \n    public static int findLeaderParent(int u){\n        return par[u]==u ? u : (par[u]=findLeaderParent(par[u]));\n    }\n    \n\n    public static int mergeUnion(int p1, int p2) {\n        if (p1 != p2) {\n            par[p2] = p1;\n            return 0;\n        }\n        return 1; // Agar cycle hui to 1 return karega\n    }\n\n    public int regionsBySlashes(String[] grid) {\n        int n = grid.length;\n        int N = n + 1;\n\n        par = new int[N * N];\n        for (int i = 0; i < N * N; i++) {\n            if (i / N == 0 || i % N == 0 || i / N == N - 1 || i % N == N - 1) { // Sare boundary elements ka parent 0 ko\n                                                                                // maan liya kyunki wo ek sath connected\n                                                                                // hain\n                par[i] = 0;\n            } else {\n                par[i] = i;\n            }\n        }\n\n        int numberOfCycles = 1; // 1 se isiliye start kiya kyunki ek cycle to already hai jo boundary hai\n        for (int i = 0; i < n; i++) {\n            String str = grid[i];\n            for (int j = 0; j < str.length(); j++) {\n                char ch = str.charAt(j);\n                if (ch == \'/\') {\n                    // Dono points nikale ki kahan pe line draw hogi aur tab merge kara\n                    int p1 = findLeaderParent(i * N + j + 1);\n                    int p2 = findLeaderParent((i + 1) * N + j);\n                    numberOfCycles += mergeUnion(p1, p2);\n                } else if (ch == \'\\\\\') {\n                    int p1 = findLeaderParent(i * N + j);\n                    int p2 = findLeaderParent((i + 1) * N + j + 1);\n                    numberOfCycles += mergeUnion(p1, p2);\n                }\n            }\n        }\n\n        return numberOfCycles;\n    }\n```\n\n![image](https://assets.leetcode.com/users/images/2c379413-bfd3-49ab-a8a0-c38fef102170_1663906020.952561.png)\n\n![image](https://assets.leetcode.com/users/images/be32b0ba-ab4f-471b-8450-af1f06e39abc_1663906050.509046.png)\n\n![image](https://assets.leetcode.com/users/images/0b8eb4dc-d83c-4a65-b247-3fd1bd94ccdf_1663906104.5362945.png)\n\n![image](https://assets.leetcode.com/users/images/4291da48-e979-4606-b21e-1d8b9229124d_1663906137.050491.png)\n\n![image](https://assets.leetcode.com/users/images/96637bab-d66f-4cf0-be96-42753eb8abcd_1663906329.5201976.png)\n\n![image](https://assets.leetcode.com/users/images/65ff557b-2ab9-4dbb-8b32-3405b2af7536_1663906342.911261.png)\n\n![image](https://assets.leetcode.com/users/images/2efc8945-2ee5-4d99-9050-beecbc9855bd_1663906346.8814092.png)\n\n![image](https://assets.leetcode.com/users/images/13f3f4bb-8821-4b26-a039-1ccd6a078156_1663906351.6488264.png)\n\n![image](https://assets.leetcode.com/users/images/df56ad37-b66a-4522-b20a-f45ac8881cad_1663906360.004411.png)\n\n\n
['Union Find', 'Graph', 'Java']