Easy Java Solution | Using HashMap & HashSet
\r\n# Complexity\r\n- Time complexity: O(nlogn) (For the Tree Set Used & Sorting is done)\r\n Add your time complexity here, e.g. O(n) \r\n\r\n- Space complexit
\r\n# Complexity\r\n- Time complexity: $$O(nlogn)$$ *(For the Tree Set Used & Sorting is done)*\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity: $$O(n)$$\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>> orders) {\r\n        TreeSet<String> ts= new TreeSet<>();\r\n        HashMap<String, HashMap> table= new HashMap<>();\r\n\r\n        for(List<String> l1: orders){\r\n            String tableNo= l1.get(1);\r\n            String item= l1.get(2);\r\n            ts.add(item);\r\n\r\n            if(table.containsKey(tableNo)){\r\n                HashMap<String, Integer> tableOrder= table.get(tableNo); \r\n                tableOrder.put(item, tableOrder.getOrDefault(item, 0)+1);\r\n            }\r\n            else{\r\n                HashMap<String, Integer> tableOrder= new HashMap<>();\r\n                tableOrder.put(item, 1);\r\n                table.put(tableNo, tableOrder);\r\n            }\r\n        }\r\n\r\n        List<List<String>> result= new ArrayList<>();\r\n        List<String> row0= new ArrayList<>();\r\n        row0.add("Table");\r\n\r\n        // System.out.println(ts);\r\n        for(String s: ts){\r\n            row0.add(s);\r\n        }\r\n\r\n        // result.add(row0);\r\n\r\n        for(String s: table.keySet()){\r\n            List<String> row= new ArrayList<>();\r\n\r\n            row.add(s);\r\n            HashMap<String, Integer> tableOrder= table.get(s);\r\n\r\n            for(int i=1; i<row0.size(); i++){\r\n                String item= row0.get(i);\r\n\r\n                if(tableOrder.containsKey(item)){\r\n                    row.add( Integer.toString(tableOrder.get(item)) );\r\n                }\r\n                else{\r\n                    row.add("0");\r\n                }\r\n            }\r\n            result.add(row);\r\n        }\r\n\r\n        Collections.sort(result, (r1, r2) ->{\r\n            int tableNo1= Integer.parseInt(r1.get(0));\r\n            int tableNo2= Integer.parseInt(r2.get(0));\r\n\r\n            if(tableNo1 < tableNo2){\r\n                return -1;\r\n            }\r\n            else if(tableNo1 > tableNo2){\r\n                return 1;\r\n            }\r\n            else{\r\n                return 0;\r\n            }\r\n        });\r\n\r\n        result.add(0, row0);\r\n        return result;\r\n    }\r\n}\r\n```\r\n![please-upvote.jpg](https://assets.leetcode.com/users/images/2e9f2562-0165-4a29-8d0b-6814ecc75203_1683364601.6944568.jpeg)\r\n
['Hash Table', 'String', 'Java']