Best Java Solution || Beats 95%
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n\r\n# Approach\r\n Describe your approach to solving the problem. \r\n\r\n# Complex
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution {\r\n    public List<List<String>> displayTable(List<List<String>> arr) {\r\n        HashMap<Integer,HashMap<String,Integer>> hp = new HashMap<>();\r\n        ArrayList<String> str = new ArrayList<>();\r\n        HashSet<String> hs = new HashSet<>();\r\n\r\n        for(var x : arr){\r\n            int a = Integer.valueOf(x.get(1));\r\n            String k = x.get(2);\r\n\r\n            if(!hs.contains(k)) str.add(k);\r\n\r\n            hs.add(k);\r\n\r\n            if(hp.containsKey(a)){\r\n                HashMap<String,Integer> h = hp.get(a);\r\n                if(h.containsKey(k)){\r\n                    h.put(k,h.get(k)+1);\r\n                }else{\r\n                    h.put(k,1);\r\n                }\r\n            }else{\r\n                HashMap<String,Integer> h = new HashMap<>();\r\n                h.put(k,1);\r\n                hp.put(a,h);\r\n            }\r\n        }\r\n\r\n        Collections.sort(str);\r\n        List<List<String>> ans = new ArrayList<>();\r\n        List<String> ar = new ArrayList<>();\r\n        ar.add("Table");\r\n        for(var x : str){\r\n            ar.add(x);\r\n        }\r\n        ans.add(ar);\r\n\r\n        ArrayList<Integer> table = new ArrayList<>();\r\n\r\n        for(var x : hp.keySet()){\r\n            table.add(x);\r\n        }\r\n\r\n        \r\n        Collections.sort(table);\r\n\r\n        for(int i=0; i<table.size(); i++){\r\n            int x = table.get(i);\r\n            List<String> a = new ArrayList<>();\r\n            a.add(String.valueOf(x));\r\n            for(int j=0; j<str.size(); j++){\r\n                String k = str.get(j);\r\n                HashMap<String,Integer> h = hp.get(x);\r\n                if(h.containsKey(k)){\r\n                    a.add(String.valueOf(h.get(k)));\r\n                }else a.add("0");\r\n            }\r\n            ans.add(a);\r\n        }\r\n        return ans;\r\n    }\r\n}\r\n```
['Java']