Find Topo sort and compare | Easy to understand
\nclass Solution {\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        int rowTopo[] = topoSort(k,rowConditions);\n 
```\nclass Solution {\n    public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {\n        int rowTopo[] = topoSort(k,rowConditions);\n        int colTopo[] = topoSort(k,colConditions);\n        int ans[][] = new int[k][k];\n        if(rowTopo.length==0||colTopo.length==0)\n            return new int[0][0];\n        for(int i=1;i<=k;i++){\n            for(int j=1;j<=k;j++){\n                if(rowTopo[i]==colTopo[j]){\n                    ans[i-1][j-1] = rowTopo[i];\n                }\n            }\n        }\n        return ans;\n    }\n    public int[] topoSort(int V, int[][]node){\n        \n        ArrayList<ArrayList<Integer>> adj = new ArrayList<>();\n        int []rank = new int[V+1];\n        int ans[] = new int[V+1];\n        for(int i=0;i<=V;i++){\n            adj.add(new ArrayList<>());\n        }\n        for(int n[] : node){\n            adj.get(n[0]).add(n[1]);\n             rank[n[1]]++;\n        }\n        \n        Queue<Integer> q = new LinkedList<>();\n        for(int i=0;i<rank.length;i++){\n            if(rank[i]==0){\n              q.add(i);\n            }\n        }\n        int len = 0;\n        while(!q.isEmpty()){\n            int temp = q.poll();\n            ans[len++] = temp;\n            for(int next : adj.get(temp)){\n                rank[next]--;\n                if(rank[next]==0){\n                    q.add(next);\n                }\n            }\n            \n        }\n        if(len!=V+1)\n            return new int[0];\n        return ans;\n    }\n}\n```
['Graph', 'Topological Sort', 'Matrix', 'Java']