JAVA Solution Explained in HINDI
https://youtu.be/HmnmBjjyZeo\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote 
https://youtu.be/HmnmBjjyZeo\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\n## Subscribe Goal:- 500\n## Current Subscriber:- 499\n\n# Complexity\n Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n - Constructing the graph: O(k + e), where e is the number of edges.\nTopological sorting: O(k + e) for each graph.\nConstructing the matrix: O(k^2) due to the nested loops for filling the matrix.\n\n Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- O(k + e) for storing the graph.\nO(k) for storing the topological sort result.\nO(k^2) for the final matrix.\n\n# Code\n```\nclass Solution {\n    public List<Integer> topologicalOrder(int k, List<List<Integer>> gr) {\n        int[] indeg = new int[k + 1];\n        for (int node = 1; node <= k; node++) {\n            for (int child : gr.get(node)) {\n                indeg[child]++;\n            }\n        }\n        Queue<Integer> q = new ArrayDeque<>();\n        boolean[] vis = new boolean[k + 1];\n        for (int node = 1; node <= k; node++) {\n            if (indeg[node] == 0) {\n                q.add(node);\n            }\n        }\n\n        List<Integer> topoSort = new ArrayList<>();\n        while (!q.isEmpty()) {\n            int sz = q.size();\n            while (sz-- > 0) {\n                int node = q.poll();\n                vis[node] = true;\n                topoSort.add(node);\n\n                for (int child : gr.get(node)) {\n                    if (!vis[child]) {\n                        indeg[child]--;\n                        if (indeg[child] == 0) {\n                            q.add(child);\n                        }\n                    }\n                }\n            }\n        }\n        for (int node = 1; node <= k; node++) {\n            if (indeg[node] != 0) {\n                return new ArrayList<>();\n            }\n        }\n        return topoSort;\n    }\n\n    public List<List<Integer>> buildGraph(int k, int[][] conditions) {\n        List<List<Integer>> gr = new ArrayList<>();\n        for (int i = 0; i <= k; i++) {\n            gr.add(new ArrayList<>());\n        }\n\n        for (int[] condition : conditions) {\n            int u = condition[0], v = condition[1];\n            gr.get(u).add(v);\n        }\n        return gr;\n    }\n\n    public void fillTopoArray(int k, List<Integer> rowTopo) {\n        Set<Integer> vis = new HashSet<>(rowTopo);\n\n        for (int el = 1; el <= k; el++) {\n            if (!vis.contains(el)) {\n                rowTopo.add(el);\n            }\n        }\n    }\n\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        List<List<Integer>> rowGr = buildGraph(k, rowConditions);\n        List<List<Integer>> colGr = buildGraph(k, colConditions);\n\n        List<Integer> rowTopo = topologicalOrder(k, rowGr);\n        List<Integer> colTopo = topologicalOrder(k, colGr);\n\n        if (rowTopo.isEmpty() || colTopo.isEmpty()) {\n            return new int[0][0];\n        }\n\n        fillTopoArray(k, rowTopo);\n        fillTopoArray(k, colTopo);\n\n        Map<Integer, Integer> colInd = new HashMap<>();\n        for (int j = 0; j < k; j++) {\n            colInd.put(colTopo.get(j), j);\n        }\n\n        int[][] ans = new int[k][k];\n        for (int i = 0; i < k; i++) {\n            ans[i][colInd.get(rowTopo.get(i))] = rowTopo.get(i);\n        }\n        return ans;\n    }\n}\n```
['Java']