Python3 O(NlogN) solution with dictionaries (98.54% Runtime)
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n\r\nUse a dictionary to store mappings of table -> menus\r\n\r\n# Approach\r\n Desc
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n![image.png](https://assets.leetcode.com/users/images/37f329bb-fecc-4491-9c00-a3cc6c2a8766_1702472418.9787211.png)\r\nUse a dictionary to store mappings of table -> menus\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n- Define a dictionary to store table -> menus mapping\r\n- Iteratively update the dictionary\r\n- Define an array for your answer and append schema row\r\n- Iteratively add corresponding menus per table with sorted order\r\n- Return your answer\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\nIf this solution is similar to yours or helpful, upvote me if you don\'t mind\r\n```\r\nclass Solution:\r\n    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:\r\n        d = {}\r\n        menus = set()\r\n\r\n        for _, tn, fi in orders:\r\n            if tn not in d:\r\n                d[tn] = {}\r\n\r\n            if fi not in d[tn]:\r\n                d[tn][fi] = 1\r\n            else:\r\n                d[tn][fi] += 1\r\n\r\n            menus.add(fi)\r\n\r\n        menus = sorted(list(menus))\r\n        ans = [["Table"] + menus]\r\n\r\n        for key in sorted([int(x) for x in list(d.keys())]):\r\n            k = str(key)\r\n            r = [k]\r\n            \r\n            for m in menus:\r\n                if m not in d[k]:\r\n                    r.append("0")\r\n                else:\r\n                    r.append(str(d[k][m]))\r\n\r\n            ans.append(r)\r\n\r\n        return ans\r\n```
['Array', 'Hash Table', 'String', 'Sorting', 'Python', 'Python3']