Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
 
Example 1:
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]] 
Explanation:
The displaying table looks like:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0
For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
For the table 5: Carla orders "Water" and "Ceviche".
For the table 10: Corina orders "Beef Burrito". 

Example 2:
Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]] 
Explanation: 
For the table 1: Adam and Brianna order "Canadian Waffles".
For the table 12: James, Ratesh and Amadeus order "Fried Chicken".

Example 3:
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]

 
Constraints:

1 <= orders.length <= 5 * 10^4
orders[i].length == 3
1 <= customerNamei.length, foodItemi.length <= 20
customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
tableNumberi is a valid integer between 1 and 500.

Difficulty: interview

Starter code:

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        

Solutions:
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        answer = []
        
        if orders is None or len(orders) == 0 or len(orders[0]) == 0:
            return answer
        
        items = set()
        
        for order in orders:
            items.add(order[2])
            
        items = sorted(list(items))
        answer.append([\"Table\"])
        
        for item in items:
            answer[0].append(item)
            
        hashMap = collections.defaultdict(list)
        itemIndex = self.convertItemToIndex(answer[0])
        m = len(answer[0])
        
        for order in orders:
            hashMap[int(order[1])].append(order[2])
            
        hashMap = {key : hashMap[key] for key in sorted(hashMap.keys())}
        currentIndex = 1
        
        for key in hashMap.keys():
            answer.append([\"0\"] * m)
            answer[currentIndex][0] = str(key)
            countMap = defaultdict(lambda: 0)
            
            for currentItem in hashMap[key]:
                countMap[currentItem] += 1
            
            for currentItem in countMap.keys():
                answer[currentIndex][itemIndex[currentItem]] = str(countMap[currentItem])
            
            currentIndex += 1
            
        return answer
    
    def convertItemToIndex(self, tempAns):
        itemToIndex = {}
        
        for i, curr in enumerate(tempAns):
            itemToIndex[curr] = i
            
        return itemToIndex
            
                    

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        
#         food = dict()
        
#         for order in orders:
            
#             name, table, item = order[0], order[1], order[2]
            
#             if item not in food:
#                 food[item] = {}
#             if table not in food[item]:
#                 food[item][table] = 0
#             food[item][table] += 1
        
#         print(food)
#         dishes = [[\"Table\"] + sorted(food)]
        
#         print(dishes)
        
#         rowSize = len(dishes[0])
#         for i, f in enumerate(sorted(food)):
#             l = ['0'] * rowSize
#             tables = food[f]
#             for table in sorted(tables):
#                 l[0] = table
#                 l[i+1] = str(tables[table])
#             dishes.append(l)
#         print(dishes)
        
        tables = dict()
        
        foodSet = set()
        for order in orders:
            
            name, table, item = order[0], order[1], order[2]
            foodSet.add(item)
            if int(table) not in tables:
                tables[int(table)] = {}
            if item not in tables[int(table)]:
                tables[int(table)][item] = 0
            tables[int(table)][item] += 1
        
        row = [\"Table\"] + sorted(foodSet)
        dishes = [row]
        index = dict()
        
        for i, r in enumerate(row):
            index[r] = i
            
        rowSize = len(dishes[0])
        
        for table in sorted(tables):
     
            foods = tables[table]
            l = [\"0\"] * rowSize
            l[0] = str(table)
            for food in sorted(foods):
                l[index[food]] = str(foods[food])
       
            dishes.append(l)
        return dishes
        

from collections import defaultdict

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        record = defaultdict(lambda: defaultdict(int))
        for _, table, item in orders:
            record[table][item] += 1
        
        res = []
        items = sorted(set(i for v in list(record.values()) for i in list(v.keys())))
        
        for table, v in list(record.items()):
            res.append([table] + ['0' if i not in v else str(v[i]) for i in items])
        
        res.sort(key=lambda x: [int(i) for i in x])
        res = [['Table'] + items] + res
        return res


class Solution:
    def displayTable(self, orders):
        food, table = [0]*len(orders), ['']*len(orders)
        
        for i, item in enumerate(orders):
            table[i], food[i] = int(item[1]), item[2]
            
        food = sorted(set(food))
        table = sorted(set(table))
        
        dic = {str(key): [0]*len(food) for key in table}
        
        for item in orders:
            dic[item[1]][food.index(item[2])] += 1
            
        res = [['Table'] + food]
        for key, val in dic.items():
            res.append([key] + list(map(str, val)))
            
        return res

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        Items = []
        for item in orders:
            if item[2] not in Items:
                Items.append(item[2])
                
        Items = [\"Table\"] + sorted(Items)
        
        dic = {}
        for table in orders:
            if table[1] not in dic:
                dic[table[1]] = [table[1]] + ['0']*(len(Items)-1)
        
        for order in orders:
            table = order[1]
            item = order[2]
            idx = Items.index(item)
            temp = int(dic[table][idx]) + 1
            dic[table][idx] = str(temp)
            
            
        temp = []
        for key in dic:
            temp.append(dic[key])
            
        ans = sorted(temp, key = lambda x: int(x[0]))
        
        return [Items] + ans
        
        
        
        
            
                
        
                
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food, table = [0]*len(orders), ['']*len(orders)

        for i, item in enumerate(orders):
            table[i], food[i] = int(item[1]), item[2]

        food = sorted(set(food))
        table = sorted(set(table))
     
        my_dict = {str(key): [0]*len(food) for key in table}

        for item in orders:
            my_dict[item[1]][food.index(item[2])] += 1
        
        result = [['Table'] + food]
        for key, val in my_dict.items():
            result.append([key]+list(map(str,val)))
        
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        Items = []
        for item in orders:
            if item[2] not in Items:
                Items.append(item[2])
                
        Items = [\"Table\"] + sorted(Items)
        
        dic = {}
        for table in orders:
            if table[1] not in dic:
                dic[table[1]] = [table[1]] + ['0']*(len(Items)-1)
        
        for order in orders:
            table = order[1]
            item = order[2]
            idx = Items.index(item)
            temp = int(dic[table][idx]) + 1
            dic[table][idx] = str(temp)

        temp = []
        for key in dic:
            temp.append(dic[key])
        ans = sorted(temp, key = lambda x: int(x[0]))
        
        return [Items] + ans
        
        
        
        
            
                
        
                
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        keys, table = [], []
        for order in orders:
            keys.append(order[2])
            table.append(int(order[1]))
        keys = sorted(set(keys))
        table = [str(i) for i in sorted(set(table))] 
        d = {t : {k : 0 for k in keys} for t in table}
        for order in orders:
            d[order[1]][order[2]] += 1
        ans = [[\"Table\"] + keys]
        for tab in table:
            temp = [tab]
            for food in keys:
                temp.append(str(d[tab][food]))
            ans.append(temp)
        return ans

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        desk = collections.defaultdict(collections.Counter)
        meal = set()
        for _, table, food in orders:
            meal.add(food)
            desk[table][food] += 1
        foods = sorted(meal)
        result = [['Table'] + [food for food in foods]]
        for table in sorted(desk, key=int):
            result.append([table] + [str(desk[table][food]) for food in foods])
        return result       

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        dishes = list({order[2] for order in orders})
        dishes.sort()
        res = [[\"Table\"]]
        res[0].extend(dishes)
        hashmap = collections.defaultdict(dict)
        for o in orders:
            if o[2] in hashmap[o[1]]:
                hashmap[o[1]][o[2]] += 1
            else:
                hashmap[o[1]][o[2]] = 1
        
        cust = sorted({int(c[1]) for c in orders})
        for c in cust:
            no = str(c)
            lst = []
            order = hashmap[no]
            for od in dishes:
                if od in order:
                    lst.append(str(order[od]))
                else:
                    lst.append(\"0\")
            lst = [no] + lst
            res.append(lst)
        return res
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tableOrders = defaultdict(lambda: defaultdict(int))
        for order in orders:
            name,table,food = order
            tableOrders[table][food] += 1
        
        foods = set()
        for table in tableOrders:
            for key in tableOrders[table]:
                foods.add(key)
        foods = sorted(list(foods))
        tables = sorted(list(tableOrders.keys()),key=lambda x: int(x))
        # print(tables)
        result = [[\"Table\"]+foods]
        for table in tables:
            toAppend = [table]
            for food in foods:
                toAppend.append(str(tableOrders[table][food]))
            result.append(toAppend)
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = collections.defaultdict(dict)
        food = set()
        for _,i,j in orders:
            table[i][j] = table[i].get(j, 0)+1
            food.add(j)
        
        res = [ ['Table']+sorted(food) ]
        for i in sorted(table, key=lambda x:int(x)):
            tmp = [ str(i) ]
            for j in range(1, len(res[0])):
                tmp.append( str( table[str(i)].get(res[0][j], 0) ) )
            res.append(tmp)
        
        return res


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        desk = collections.defaultdict(collections.Counter)
        meal = set()
        for _, table, food in orders:
            meal.add(food)
            desk[table][food] += 1
        foods = sorted(meal)
        result = [['Table'] + [food for food in foods]]
        for table in sorted(desk, key=int):
            result.append([table] + [str(desk[table][food]) for food in foods])
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table_numbers =sorted(set( [int(i[1]) for i in orders]))
        items = sorted(set([i[2] for i in orders]))
        temp_dict = {str(key):[0]*len(items) for key in table_numbers}
        for item in orders: 
            temp_dict[item[1]][items.index(item[2])] = temp_dict[item[1]][items.index(item[2])]+1
        result = [['Table']+items]
        for key,val in temp_dict.items(): 
            result.append([key]+list(map(str,val)))
        return(result)

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        table = {}
        dishes = set()
        
        for order in orders:
            if order[1] not in table:
                table[order[1]] = {}
                
            if order[2] not in table[order[1]]:
                table[order[1]][order[2]] = 1
            else:
                table[order[1]][order[2]] += 1
                
            dishes.add(order[2])
            
        dishes = sorted(list(dishes))
        table_ints = [int(x) for x in table.keys()]
        table_nums = sorted(table_ints, reverse=False)
        
        template = [\"Table\"] + dishes
        display = [template]
        for num in table_nums:
            order = [str(num)]
            for dish in dishes:
                order.append(str(table.get(str(num), {}).get(dish, 0)))
                
            display.append(order)
            
        return display

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        # runtime: O(nlogn + mlogm), where n = number of unique tables and m = # of unique foods
        # space: O(mn)
        foods = set()
        tables = set()
        for o in orders:
            tables.add(int(o[1]))
            foods.add(o[2])
        tables = sorted(tables)
        foods = sorted(foods)
        result = [([0] * (len(foods) + 1)) for i in range(len(tables) + 1)]
        # print(result)
        result[0][0] = \"Table\"
        reverse_foods = {}
        for i, food in enumerate(foods):
            result[0][i + 1] = food
            reverse_foods[food] = i + 1
        reverse_tables = {}
        for i, table in enumerate(tables):
            result[i + 1][0] = table
            reverse_tables[table] = i + 1
        for o in orders:
            result[reverse_tables[int(o[1])]][reverse_foods[o[2]]] += 1
        for i in range(1, len(result)):
            for j in range(len(result[0])):
                result[i][j] = str(result[i][j])
        return result
        
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = {}
        dishes = {}
        for name, table, dish in orders:
            if table not in tables:
                tables[table] = 0
            if dish not in dishes:
                dishes[dish] = 0
        tables = sorted(tables.keys(), key=lambda x: int(x))
        dishes = sorted(dishes.keys())
        tables.insert(0, 'Table')
        dishes.insert(0, 'Table')
        nrows = len(tables)
        ncolumns = len(dishes)
        
        results = [[0 for j in range(ncolumns)] for i in range(nrows)]
        results[0] = dishes
        tables = {table:index for index, table in enumerate(tables)}
        dishes = {dish:index for index, dish in enumerate(dishes)}

        for name, table, dish in orders:
            i = tables[table]# + 1
            j = dishes[dish]# + 1
            results[i][0] = table
            results[i][j] = results[i][j] + 1
        #print(results)
        for i in range(nrows):
            for j in range(ncolumns):
                results[i][j] = str(results[i][j])
        return results

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food=[]
        t={}
        disp=[]
        for i in orders:
            food.append(i[2])
            if i[1] in t:
                t[i[1]].append(i[2])
            else:
                t[i[1]]=[]
                t[i[1]].append(i[2])
        tmp =[\"Table\"]
        food=sorted(set(food))
        tmp.extend(food)
        disp.append(tmp)

        for k in sorted(t.keys(), key=lambda i:int(i)):
            tmp=[]
            tmp.append(k)
            for f in food:
                foodct=0
                if f in t[k]:
                    foodct = t[k].count(f)
                tmp.append(str(foodct))
            disp.append(tmp)
        print(disp)
        return disp


from collections import Counter, defaultdict
class Solution:
    def displayTable(self, orders):
        foods = set()
        table_to_food = defaultdict(Counter)
        for customer, table, food in orders:
            table_to_food[table][food] += 1
            foods.add(food)
        foods = sorted(foods)
        m = len(table_to_food)
        n = len(foods)
        res = [[None for j in range(n + 1)] for i in range(m + 1)] 
        res[0] = ['Table'] + foods 
        for i, table in enumerate(sorted(table_to_food.keys(), key=int)):
            res[i + 1][0] = str(table)
            for j in range(0, n):
                food = foods[j]
                res[i + 1][j + 1] = str(table_to_food[table][food])
        return res


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        foods = set()
        tables = set()
        
        for _, table, food in orders:
            foods.add(food)
            tables.add(table)
        
        foodcounts = {f:{t:0 for t in tables} for f in foods}
        for _, table, food in orders:
            foodcounts[food][table] += 1
            
        output = []
        output.append([\"Table\"] + sorted(foods))
        for table in sorted(map(int,tables)):
            table = str(table)
            counts = [table]
            for food in sorted(foods):
                counts.append(str(foodcounts[food][table]))
            output.append(counts)
        return output
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        dishes = list({order[2] for order in orders})
        dishes.sort()
        res=[[\"Table\"]]
        res[0].extend(dishes)
        hashmap=defaultdict(dict)
        for order in orders:
            if order[2] in hashmap[order[1]]:
                hashmap[order[1]][order[2]]+=1
            else:
                hashmap[order[1]][order[2]]=1
        cust = sorted({int(c[1]) for c in orders})
        for cus in cust:
            cus=str(cus)
            alist = []
            order = hashmap[cus]
            for dish in dishes:
                if dish in order:
                    alist.append(str(order[dish]))
                else:
                    alist.append(\"0\")
            alist=[cus] + alist
            res.append(alist)
        return res


# Dict of Dicts For food counts on a table
#   tables[5][\"Ceviche\"] = 1
# Set unique foods
# 1. Time O(N) Space: O(N)
# 2. Time O(I*J)


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        table_foods = collections.defaultdict(lambda: collections.defaultdict(int))
        unique_food = set()
        
        for _, table_number, food in orders:
            table_foods[int(table_number)][food] += 1
            unique_food.add(food)
        
        headers = sorted(unique_food)
        data = [[\"Table\"] + headers]
        
        for table_number in sorted(table_foods.keys()):
            table_row = [str(table_number)]
            for food in headers:
                table_row.append(str(table_foods[table_number][food]))
            data.append(table_row)
        return data

    

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        desk = collections.defaultdict(collections.Counter)
        meal = set()
        for _, table, food in orders:
            meal.add(food)
            desk[table][food] += 1
        foods = sorted(meal)
        result = [['Table'] + [food for food in foods]]

        for table in sorted(desk,key = int):
            result.append([table] + [str(desk[table][food]) for food in foods])
        return result
        
        


# Dict of Dicts For food counts on a table
#   tables[5][\"Ceviche\"] = 1
# Set unique foods
# 1. Time O(N) Space: O(N)
# 2. Time O(I*J)

from collections import defaultdict

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        table_foods = defaultdict(lambda: defaultdict(int))
        unique_food = set()
        
        for _, table_number, food in orders:
            table_foods[int(table_number)][food] += 1
            unique_food.add(food)
        
        headers = sorted(unique_food)
        data = [[\"Table\"] + headers]
        
        for table_number in sorted(table_foods.keys()):
            table_row = [str(table_number)]
            for food in headers:
                table_row.append(str(table_foods[table_number][food]))
            data.append(table_row)
        return data

    

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = {}
        dishes = {}
        for name, table, dish in orders:
            if table not in tables:
                tables[table] = 0
            if dish not in dishes:
                dishes[dish] = 0
        tables = sorted(tables.keys(), key=lambda x: int(x))
        dishes = sorted(dishes.keys())
        results = [[\"0\" for j in range(len(dishes) + 1)] for i in range(len(tables) + 1)]
        results[0] = [\"Table\"] + dishes
        for name, table, dish in orders:
            i = tables.index(table) + 1
            j = dishes.index(dish) + 1
            results[i][0] = table
            results[i][j] = str(int(results[i][j]) + 1)
        return results

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        mytables = set()
        
        header = set(\" \")
        mytablefood = collections.Counter()
        
        for customerName, table, food in orders:
            header.add(food)
            mytables.add(table)
            mytablefood[table+food] += 1
        
        header = list(header)
        header.sort()
        header[0] = \"Table\"
        output = [header]
        
        mytables = sorted(mytables, key=int)
        for table in mytables:
            T = [table]
            for food in header[1:]:
                T.append(str(mytablefood[table+food]))
            output.append(T)
        return output

from collections import defaultdict
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        result = []
        table_dct = defaultdict(list)
        items = set()
        tables = set()
        for order in orders:
            table_dct[order[1]].append(order[2])
            items.add(order[2])
            tables.add(int(order[1]))
        items = sorted(list(items))
        tables = sorted(list(tables))
        result.append(['Table'] + items)
        for table in tables:
            table_result = [str(table)]
            for item in items:
                table_result.append(str(table_dct[str(table)].count(item)))
            result.append(table_result)
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = {}        
        header = []
        result = []
        for i,lst in enumerate(orders):   
            if lst[2] not in header:
                header.append(lst[2])
            tmp_lst = table.get(int(lst[1]),[])
            tmp_lst.append(lst[2]) 
            table[int(lst[1])] = tmp_lst    
            
        header.sort()      
        
        for k in table:
            tmp_lst = []
            tmp_lst.append(k)
            for pos in header:                         
                tmp_lst.append(table[k].count(pos))
            result.append(tmp_lst)
        result.sort()       
        header.insert(0,\"Table\")
        result.insert(0,header)
        
        for i,lst in enumerate(result):
            result[i] = list(map(str,lst))
    
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        items_all = sorted(list(set([ele[2] for ele in orders])))
        tb_disp = {}
        for od in orders:
            tbn = od[1]
            itm = od[2]
            if tbn not in tb_disp:
                tb_disp[tbn] = [0 for _ in range(len(items_all))]
            tb_disp[tbn][items_all.index(itm)] +=1
        ans_li = sorted([[tb,itm] for tb,itm in tb_disp.items()],key=lambda x:int(x[0]))
        ans = [[\"Table\"]+items_all]
        for ele in ans_li:
            temp = [str(ele[0])]
            for it in ele[1]:
                temp.append(str(it))
            ans.append(temp)
        return ans
            
        
        
                

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        tables = {} 
        menu = set()
        
        for o in orders:
            num, food = int(o[1]), o[2]
            
            if num not in tables:
                tables[num] = {}
            
            if food not in tables[num]:
                tables[num][food] = 0 
            
            tables[num][food] += 1 
            
            if food not in menu:
                menu.add(food)
        
        menu = sorted(menu)
        res = [['Table'] + menu] 
        
        for table_num in sorted(tables, key=lambda n:n):
            row = [str(table_num)]
            table_order = tables[table_num]

            for food in menu:
                food_count = table_order.get(food, 0)
                row.append(str(food_count)) 
            res.append(row)
        
        return res 

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        dishes = []
        dishesSet = set()
        tables = []
        tableSet = set()
        
        orderDict = {}
        
        for order in orders:
            tableID = int(order[1])
            
            if tableID not in tableSet:
                tableSet.add(tableID)
                tables.append(tableID)
                orderDict[tableID] = {}
            
            if order[2] not in dishesSet:
                dishesSet.add(order[2])
                dishes.append(order[2])
            
            orderDict[tableID][order[2]] = orderDict[tableID].get(order[2], 0) + 1
        
        dishes.sort()
        tables.sort()
            
        orderTable = [[\"Table\"] + dishes]
        
        for table in tables:
            curr = []
            curr.append(str(table))
            for dish in dishes:
                if dish in orderDict[table]:
                    curr.append(str(orderDict[table][dish]))
                else:
                    curr.append(\"0\")
            
            orderTable.append(curr)
        
        return orderTable
        
        
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = OrderedDict()
        
        for k in orders:
            if k[1] not in table:
                table[k[1]]={}
            
            if k[2] in table[k[1]]:
                table[k[1]][k[2]] += 1
            else:
                table[k[1]][k[2]] = 1
        
        d_order = sorted([int(i) for i in table.keys()])
        
        d_order = [str(i) for i in d_order]
        
        f_order = sorted(set([k[2] for k in orders]))
        
        print(d_order, f_order)
        print(table)
        res = []
        
        res.append([\"Table\"] + f_order)
        
        for k in d_order:
            tmp = [k]
            for _k in f_order:
                if _k in table[k]:
                    tmp.append(str(table[k][_k]))
                else:
                    tmp.append(\"0\")
            res.append(tmp)
        return res

            
            

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:

        
        table = []
        food = []
        
        for item in orders:
            table.append(int(item[1]))
            food.append(item[2])

        
        food = sorted(list(set(food)))
        table = sorted(list(set(table)))
        
        
        
        
        #my_dict = {str(key): ['0']*len(food) for key in table}
        my_dict = {str(key): [0]*len(food) for key in table}



        
        for item in orders:
            my_dict[item[1]] [food.index(item[2])] = my_dict[item[1]][food.index(item[2])] +  1
            # my_dict[item[1]] [food.index(item[2])] = int(my_dict[item[1]][food.index(item[2])]) +  1
            # my_dict[item[1]][food.index(item[2])] = str(my_dict[item[1]][food.index(item[2])])


        
        
            
            
        
        header = ['Table'] + food
        
        footer = []
        for key, val in list(my_dict.items()):
            # footer.append([key]+val)
            footer.append([key]+list(map(str,val)))

  
        
        return [header]+footer


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
      table_dicts = {}
      dicts = {}
      for order in orders:
        if (int)(order[1]) not in table_dicts: table_dicts[(int)(order[1])] = 1
        if order[2] not in dicts:
          dicts[order[2]] = {order[1]:1}
        else:
          if order[1] in dicts[order[2]]:
            dicts[order[2]][order[1]] += 1
          else:
            dicts[order[2]][order[1]] = 1
      tables = []
      for key,value in table_dicts.items(): tables.append(key)
      tables.sort()
      tables = [str(x) for x in tables]
      dishes = []
      for key,value in dicts.items(): dishes.append(key)
      dishes.sort()
      res = []
      res.append([\"Table\"] + dishes)
      for i in range(len(tables)):
        temp_res = [tables[i]]
        for dis in dishes:
          if tables[i] in dicts[dis]:
            temp_res.append((str)(dicts[dis][tables[i]]))
          else:
            temp_res.append('0')
        res.append(temp_res)
      return res
        
      
        

from collections import defaultdict
# table nuber in string
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food_set = set()
        table_orders = defaultdict(lambda : defaultdict(int))
        ans = []
        
        
        for ix, val in enumerate(orders):
            _ , table_no , food_name = val[0] , int(val[1]) , val[2]
            if table_no not in table_orders:
                table_orders[table_no][food_name] = 1
            else:
                # table number is inside
                table_orders[table_no][food_name] += 1
            food_set.add(food_name)
        
        
        temp = [\"Table\"]
        for food_name in sorted(food_set):
            temp.append(food_name)
        ans.append(temp)
        
        food_mapping = {}
        for ix in range(1,len(temp)):
            food_mapping[temp[ix]] = ix
        
        
        for table_num in sorted(table_orders.keys()):
            insert_list = [\"0\" for _ in range(len(food_mapping)+1)]
            insert_list[0] = str(table_num)
            order_dict = table_orders[table_num]
            for food_name in order_dict.keys():
                ix_add = food_mapping[food_name]
                insert_list[ix_add] = str(order_dict[food_name])
            ans.append(insert_list)
        return ans
            
            

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        menu = sorted(set([i[2] for i in orders]))
        wat = sorted(orders, key=lambda order: int(order[1]))
        wat2 = itertools.groupby(wat, key=lambda order: order[1])
        alpha_cust = [[c, [i[2] for i in cgen]] for c, cgen in wat2]
        
        ans = [['Table'] + menu]
        
        for table in alpha_cust:
            ans.append([table[0]] + [str(table[1].count(i)) for i in menu])
        return ans
        
#         print(menu)
#         print(alpha_cust)


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:

        
        table = []
        food = []
        
        for item in orders:
            table.append(int(item[1]))
            food.append(item[2])

        
        food = sorted(list(set(food)))
        table = sorted(list(set(table)))
        
        
        
        
        my_dict = {str(key): ['0']*len(food) for key in table}


        
        for item in orders:
            my_dict[item[1]][food.index(item[2])] = int(my_dict[item[1]][food.index(item[2])]) +  1
            my_dict[item[1]][food.index(item[2])] = str(my_dict[item[1]][food.index(item[2])])


        
        
            
            
        
        header = ['Table'] + food
        
        footer = []
        for key, val in list(my_dict.items()):
            #footer.append([key]+list(map(str,val)))
            footer.append([key]+val)
            

        
        result = [header]+footer
  
        
        return result


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = {}
        dishes = {}
        for name, table, dish in orders:
            if table not in tables:
                tables[table] = 0
            if dish not in dishes:
                dishes[dish] = 0
        tables = sorted(tables.keys(), key=lambda x: int(x))
        dishes = sorted(dishes.keys())
        tables.insert(0, 'Table')
        dishes.insert(0, 'Table')
        nrows = len(tables)
        ncolumns = len(dishes)
        results = [[0 for j in range(ncolumns)] for i in range(nrows)]
        results[0] = dishes
        for name, table, dish in orders:
            i = tables.index(table)# + 1
            j = dishes.index(dish)# + 1
            results[i][0] = table
            results[i][j] = results[i][j] + 1
        for i in range(nrows):
            for j in range(ncolumns):
                results[i][j] = str(results[i][j])
        return results

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = {}
        top_row = set()
        
        for order in orders:            
            _name, num, item = order
            top_row.add(item)
            if tables.get(num):
                tables[num].append(item)
            else: 
                tables[num] = [item]
                
        top_row = list(top_row)
        top_row.sort()
        top_row = [\"Table\", *top_row]
        all_items = [(top_row[idx], idx) for idx in range(len(top_row))]

        food_indexes = dict(all_items)
        
        output = []

        for table, orders in tables.items():
            row = [0 for i in range(1, len(top_row))]
            row = [table, *row]
            for food in tables[table]:
                row[food_indexes[food]] += 1;
            row = [str(num) for num in row]
            output.append(row)
        
        output.sort(key=lambda row: int(row[0]))
        output.insert(0, top_row)
        return output
        
        
        

# var displayTable = function(orders) {
#     let tables = {};
#     let allItems = new Set();
#     //get tables first, and an array of items. if table exists, push. if not, create then oush
#     for(let o of orders){
#         let [_name, num, item] = o;
#         tables[num]
#         ? tables[num].push(item)
#         : tables[num] = [item];
#         allItems.add(item);
#     }
#     let sortedItems = [\"Table\", ...[...allItems].sort()];
#     let foodIndexes = {};
#     for(let i = 1; i < sortedItems.length; i++){
#         let key = sortedItems[i];
#         foodIndexes[key] = i;
#     }

#     //then add orders to the table
#     let output = [sortedItems];
#     for(let t of Object.keys(tables)){
#         //an array from sortedItems.length, filled with zeroes
#         let row = new Array(sortedItems.length).fill(0);
#         row[0] = t;
#         // increment the food based on the index of foodIndexes
#         tables[t].forEach(f => {
#             let index = foodIndexes[f];
#             row[index] ++;
#         })
#         row = row.map(x => `${x}`);
#         output.push(row);
#     }
#     return output;
# };

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d={}
        t=set()
        for o in orders:
            if o[2] in d:
                d[o[2]].append(o[0])
            else:
                d[o[2]]=[o[0]]
                
        for o in orders:
            t.add(int(o[1]))
        dictionary_items = d.items()
        dd = sorted(dictionary_items)
        
        t=sorted(t)
        ans=[[\"Table\"]]
        
        m={}
        for o in orders:
            if o[1] in m:
                m[o[1]].append(o[2])
            else:
                m[o[1]]=[o[2]]
        
        for k,v in dd:
            ans[0].append(k)
            
        for k in t:
            ans.append([str(k)])
        
        for l in range(1,len(t)+1): # 1 -> table nums
            for y in range(1,len(ans[0])): # 0 -> food num
                ans[l].append(str(m[ans[l][0]].count(ans[0][y])))
            
        return ans
            
            
            

from collections import defaultdict

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        record = defaultdict(lambda: defaultdict(int))
        for _, table, item in orders:
            record[table][item] += 1
        
        res = []
        items = sorted(set(i for v in list(record.values()) for i in list(v.keys())))
        
        for table, v in list(record.items()):
            res.append([table] + ['0' if i not in v else str(v[i]) for i in items])
        
        res.sort(key=lambda x: int(x[0]))
        res = [['Table'] + items] + res
        return res


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d = {}
        foods = set()
        
        for x in orders:
            table = x[1]
            food = x[2]
            foods.add(food)
            
            if table not in d:
                d[table] = {}
                
            if food in d[table]:
                d[table][food] +=1 
            else:
                d[table][food] = 1
                
        print(d)
        
        foodList = list(foods)
        foodList.sort()
        foodList.insert(0,'Table')
        
        hold = foodList
        f = [hold.copy()]
        dTemp = sorted(list(d.items()),key=lambda x:int(x[0]))
        for (tableNo,tableFood) in dTemp:
            a = []
            for colName in hold:
                if colName == 'Table':
                    a.append(str(tableNo))
                elif colName in tableFood:
                    a.append(str(tableFood[colName]))
                else:
                    a.append('0')
            f.append(a)
            
        
        return f
                
                


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = []
        output = [[]]
        temp = []

        for i in range(len(orders)):
            if orders[i][1] not in table:
                table.append(orders[i][1])

        for j in range(len(table)):
            temp.append(int(table[j]))

        temp = sorted(temp)

        temp2 = []
        for k in range(len(table)):
            temp2.append(str(temp[k]))
            output.append(temp2)
            temp2 = []

        for l in range(len(orders)):
            if orders[l][2] not in output[0]:
                output[0].append(orders[l][2])

        temp2 = sorted(output[0])
        output[0] = [\"Table\"]+temp2

        for r in range(1,len(output)):
            for n in range(len(output[0])-1):
                output[r].append(\"0\")

        for m in range(len(orders)):
            tab = int(orders[m][1])
            idxt = temp.index(tab)+1
            meal = orders[m][2]
            idxm = output[0].index(meal)

            stack = int(output[idxt][idxm])
            stack += 1
            output[idxt][idxm] = str(stack)
            
        return output

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tables = []
        dishes = []
        for name, table, dish in orders:
            if table not in tables:
                tables.append(table)
            if dish not in dishes:
                dishes.append(dish)
        tables = sorted(tables, key=lambda x: int(x))
        dishes = sorted(dishes)
        results = [[\"0\" for j in range(len(dishes) + 1)] for i in range(len(tables) + 1)]
        results[0] = [\"Table\"] + dishes
        for name, table, dish in orders:
            i = tables.index(table) + 1
            j = dishes.index(dish) + 1
            results[i][0] = table
            results[i][j] = str(int(results[i][j]) + 1)
        return results

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        orders.sort(key=lambda x: int(x[1]))
        record = defaultdict(lambda: defaultdict(int))
        foods = []
        for name, table, food in orders:
            if food not in foods:
                foods.append(food)
            record[table][food] += 1

        foods = sorted(foods)
        out = [['Table'] + foods]

        for k, v in record.items():
            tmp = [k]
            for f in foods:
                tmp += [str(v.get(f)) if v.get(f) else '0']
            out.append(tmp)
        
        return out

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        ht1 = {}    #for table order
        col = []
        display = []
        
        for i, item in enumerate(orders):
            table = int(item[1])
            dish = item[2]
            if table in ht1:
                ht1[table].append(i)
            else:
                ht1[table] = [i]
            
            if dish not in col:
                col.append(dish)
        
        #to display, row is sorted tables, col is sorted dishes        
        row = list(ht1.keys())
        row.sort()
        col.sort()
        
        #initialize display matrix
        display = [[]]*(len(row)+1)
        display[0] = ['Table']
        for item in col:
            display[0].append(item)
        
        #fill out the blanks
        for i, item in enumerate(row):
            display[i+1] = [str(item)]
            for j in range(len(col)):
                display[i+1].append(0)
            for o in ht1[item]:
                dish = orders[o][2]
                c = col.index(dish)
                display[i+1][c+1] += 1
            for j in range(len(col)):
                display[i+1][j+1] = str(display[i+1][j+1])
        return display
                
                
            
            


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        ss = set()
        for (x, y, z) in orders:
            ss.add(z)
        foods = list(ss)
        foods.sort()
        foodHt = {v:i for (i, v) in enumerate(foods)}
        ans = []
        firstRow = [\"Table\"] + foods
        ans.append(firstRow)
        
        ht = {}
        for x, y, z in orders:
            if not y in ht:
                ht[y] = [0 for i in range(len(foods))]
            ht[y][foodHt[z]] += 1
        
        tables = list(ht.keys())
        tables.sort(key=lambda x :int(x))
        for t in tables:
            row = [str(t)]
            for i in range(len(foods)):
                row.append(str(ht[t][i]))
            ans.append(row)
        return ans

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:

        
        table = []
        food = []
        
        for item in orders:
            table.append(int(item[1]))
            food.append(item[2])

        
        food = sorted(list(set(food)))
        table = sorted(list(set(table)))
        
        
        
        
        my_dict = {str(key): ['0']*len(food) for key in table}
        
        print(('My Dict :',my_dict))


        
        for item in orders:
            my_dict[item[1]][food.index(item[2])] = int(my_dict[item[1]][food.index(item[2])]) +  1
            my_dict[item[1]][food.index(item[2])] = str(my_dict[item[1]][food.index(item[2])])


        
        
            
            
        
        header = ['Table'] + food
        
        footer = []
        for key, val in list(my_dict.items()):
            #footer.append([key]+list(map(str,val)))
            footer.append([key]+val)
            

        
        result = [header]+footer
  
        
        return result


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food=[]
        d=OrderedDict()
        for _,_,k in orders:
            if k not in food:
                food.append(k)
        food.sort()
        n=len(food)
        new=[(i,f) for i,f in enumerate(food)]
        for _,j,k in orders:
            if j not in d:
                d[j]=[0 for _ in range(n)]
                for i,f in new:
                    if k==f:
                        d[j][i]=1
                        break               
            else:
                for i,f in new:
                    if k==f:
                        d[j][i]+=1
                        break
        d=OrderedDict(sorted(d.items(),key=lambda x:int(x[0])))
        title=['Table']+food
        details=[[k]+[str(i) for i in v] for k,v in d.items()]
        details.insert(0,title)
        return details

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d = collections.defaultdict(collections.Counter)
        meal = set()
        for _, table, dish in orders:
            meal.add(dish)
            d[table][dish] += 1
        meal = sorted(meal)
        result = [['Table'] + [food for food in meal]]
        
        for table in sorted(d, key = int):
            result.append([table] + [str(d[table][food]) for food in meal])
        return result

from collections import defaultdict
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d = defaultdict(lambda: defaultdict(int))
        foods = set()
        for _, table, order in orders:
            d[int(table)][order]+=1
            foods.add(order)
        foods = sorted(foods)
        ret = [['Table']+foods]
        for table in sorted(d.keys()):
            ret.append([str(table)]+[str(d[table][f]) for f in foods])
        return ret

from collections import defaultdict
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        tableFood = {}
        tableFreq = {}
        foods = set()
        nums = set()
        for order in orders:
            tableNumber = order[1]
            food = order[2]
            foods.add(food)
            nums.add(tableNumber)
            idx = (tableNumber, food)
            
            
            if idx in tableFreq:
                tableFreq[idx] += 1
            else:
                tableFreq[idx] = 1
        
        cols = [\"Table\"] + sorted(foods)
        ans = [cols]
        N = len(cols)
        
        sortedKeys = sorted(nums, key = lambda k: int(k))
        
        for key in sortedKeys:
            col = [\"0\"] * N
            col[0] = key
            for i in range(1,N):
                idx = (key,cols[i])
                if idx in tableFreq:
                    col[i] = str(tableFreq[idx])
            ans.append(col)

        return ans
            
            
            
                
            
            
            
            
            
        

from collections import defaultdict


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food_items = set()
        tables = defaultdict(lambda: defaultdict(int))
        
        for o in orders:
            table_number = int(o[1])
            food_item = o[2]
            tables[table_number][food_item] += 1
            food_items.add(food_item)
            
        food_items = sorted(food_items)
        result = [[\"Table\"]]
        for food_item in food_items:
            result[0].append(food_item)
            
        for table_number in sorted(tables.keys()):
            record = [str(table_number)]            
            for food_item in food_items:
                record.append(str(tables[table_number][food_item]))
            result.append(record)
                
        return result
        

from collections import defaultdict
class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        #having a dictionary of dictionaries?
        #should have a set of all the items?
        #ex: {2:{ceviche: 1}, {burrito: 2}, 3: {ceviche: 0}}
        
        items = set()
        tableorders = defaultdict(lambda: {})
        
        for i in orders:
            table = int(i[1])
            if tableorders[table] == {}:
                tableorders[table] = defaultdict(lambda: 0)
                tableorders[table][i[2]] += 1
                items.add(i[2])
            else:
                tableorders[table][i[2]] += 1
                items.add(i[2])
    
        items = sorted(items)
        chartorders = []
        column1 = [\"Table\"]
        for i in items:
            column1.append(i)
        chartorders.append(column1)
        for c in sorted(tableorders.keys()):
            tablerow = []
            tablerow.append(str(c))
            for z in items:
                if z in tableorders[c].keys():
                    tablerow.append(str(tableorders[c][z]))
                else:
                    tablerow.append(str(0))
            chartorders.append(tablerow)
        return chartorders
        
                    

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        item_list = []
        order_dict = {}
        for order in orders:
            table = order[1]
            item = order[2]
            if item not in item_list:
                item_list.append(item)
            if int(table) not in order_dict:
                order_dict[int(table)] = []
            order_dict[int(table)].append(item)
                        
        item_list.sort()
        item_list.insert(0, 'Table')
        # print(item_list)
        order_dict = dict(sorted(order_dict.items()))
        # print(order_dict)
        
        sol = []
        sol.append(item_list)
        for table_name, table_orders in list(order_dict.items()):
            table_sol = ['0' for i in range(len(item_list))]
            table_sol[0] = table_name
            # print(table_orders)
            for order in table_orders:
                ind = item_list.index(order)
                # print(ind)
                table_sol[ind] = str(int(table_sol[ind]) + 1)
            sol.append(table_sol)
            
        for i in range(1, len(sol)):
            sol[i][0] = str(sol[i][0])
        return sol
        
        
                
                
        


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        foods = set([])
        table_idx_to_orders = {} 
        
        for order in orders:
            food = order[2]
            if food not in foods:
                foods.add(food)
                        
        foods_list = sorted(list(foods))
        total_foods = len(foods_list)
        food_to_idx = { foods_list[i] : i for i in range(len(foods_list)) }
        for order in orders:
            _, table_idx, food = order
            table_idx = int(table_idx)
            if table_idx not in table_idx_to_orders:
                table_idx_to_orders[table_idx] = [0] * total_foods
                
            table_idx_to_orders[table_idx][food_to_idx[food]] += 1
            
        result = [[\"Table\"] + list(food_to_idx)]
        print(table_idx_to_orders)
        for table_idx in sorted(table_idx_to_orders.keys()):
            print(table_idx)
            crt_orders_str = list(map(str, list(table_idx_to_orders[table_idx])))
            crt_result = [str(table_idx)] + crt_orders_str
            result.append(crt_result)
        return result
        
        

from collections import defaultdict

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        table_to_dishes = defaultdict(lambda: defaultdict(int))
        dishes = set()
        
        for order in orders:
            table = int(order[1])
            dish = order[2]
            
            table_to_dishes[table][dish] += 1
            dishes.add(dish)
            
        headers = ['Table']
        headers += [dish for dish in sorted(dishes)]
        
        result = [headers]
        
        # print (table_to_dishes.keys())
        
        for table in sorted(table_to_dishes.keys()):
            table_order = [str(table)]
            
            for ind in range(1, len(headers)):
                dish = headers[ind]
                table_order.append(str(table_to_dishes[table][dish]))
            
            result.append(table_order)
            
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        hm = {}
        food = sorted(set([x[2] for x in orders]))
        tables = sorted(set([int(x[1]) for x in orders]))
        res = [['Table']+list(food)]+[[str(x)] for x in tables]
        
        for item in orders:
            cur = hm.get(item[1], {})
            num = cur.get(item[2], 0)
            cur[item[2]] = num+1
            hm[item[1]] = cur

        for i in range(1, len(tables)+1):
            cur = hm[str(tables[i-1])]
            for f in food:
                res[i].append(str(cur.get(f, 0)))
        
        return res
            
                
        
        
        


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        display=[]
        tableList=set()
        foodList=set()
        
        for order in orders:
            foodList.add(order[2])
            tableList.add(int(order[1]))
            
        display.append([\"Table\"]+sorted(list(foodList)))
        tableList=sorted(list(tableList))
        for table in tableList:
            display.append([table]+[0]*len(foodList))
        tableToIndex={}
        for i in range(1,len(display)):
            tableToIndex[display[i][0]]=i
            
        foodToIndex={}
        for i in range(1,len(display[0])):
            foodToIndex[display[0][i]]=i
            
        for order in orders:
            display[tableToIndex[int(order[1])]][foodToIndex[order[2]]]=display[tableToIndex[int(order[1])]][foodToIndex[order[2]]]+1
            
        display=[[str(ele) for ele in table] for table in display]
                
        return display
        
            
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        unique_tables = []
        unique_dishes = []
        ans = [['Table']]
        for i in orders:
            if int(i[1]) not in unique_tables:
                unique_tables.append(int(i[1]))
            if i[2] not in unique_dishes:
                unique_dishes.append(i[2])
        unique_tables.sort()
        unique_dishes.sort()
        ans[0].extend(unique_dishes)
        for i in range(len(unique_tables)):
            listt = [unique_tables[i]]
            listt.extend([0]*len(unique_dishes))
            ans.append(listt)
        for i in orders:
            ans[unique_tables.index(int(i[1]))+1][unique_dishes.index(i[2])+1]+=1
        for i in range(len(ans)):
            for j in range(len(ans[0])):
                ans[i][j]= str(ans[i][j])
        return(ans)
                


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        table = []
        table.append(\"Table\")
        res = []
        final_ans = []
        dict = {}
        
        dish_list = []
        for dish in orders:
            if dish[2] not in dish_list:
                dish_list.append(dish[2])
        
        dish_list.sort()
        
        
        for i in range(0, len(dish_list)):
            dict[dish_list[i]] = []
            table.append(dish_list[i])
            
        final_ans.append(table)
        
        for i in range(0, len(orders)):
            dict[orders[i][2]].append(orders[i][1])
       
        table_nos = []
        for num in orders:
            if int(num[1]) not in table_nos:
                table_nos.append(int(num[1]))
        
        table_nos.sort()
        
        for i in range(0, len(table_nos)):
            op = []
            op.append(str(table_nos[i]))
            for key in dict:
                op.append(str(dict[key].count(str(table_nos[i]))))
                
                
            if op not in res:
                res.append(op)
        
        for x in res:
            final_ans.append(x)
            
        return final_ans

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        ords= defaultdict(collections.Counter)
        meals = set()
        for _, T, M in orders:
            meals.add(M)
            ords[T][M] += 1
        foods = sorted(meals)
        result = [['Table'] + [food for food in foods]]
        for T in sorted(ords, key = int):
            result.append([T] + [str(ords[T][M]) for M in foods])
        return result   
            



class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        food, table = [0]*len(orders), ['']*len(orders)
        #print(food, table)
        
        for i, item in enumerate(orders):
            table[i], food[i] = int(item[1]), item[2]
        #print(food, table)
        
        food = sorted(set(food))
        table = sorted(set(table))
        #print(food, table)
        
        my_dict = {}
        for i in table:
            my_dict[str(i)] = [0]*len(food)
        #print(my_dict)
        
        for item in orders:
            my_dict[item[1]][food.index(item[2])] += 1
        #print(my_dict)
        
        result = [['Table'] + food]
        for key, val in my_dict.items():
            result.append([key]+list(map(str,val)))
        
        return result

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = {}
        fooditems = []
        for order in orders:
            if order[1] in table:
                table[order[1]][order[2]] = table[order[1]].get(order[2],0) + 1
            
            else:
                table[order[1]] = {}
                table[order[1]][order[2]] = 1
     
            fooditems.append(order[2])
            
        fooditems = sorted(list(set(fooditems)))

        row = [[\"Table\"] + fooditems]
      
        for o in sorted(table.keys(), key=lambda x:int(x)):
         
            r = [o] + [str(table[o].get(food,0)) for food in fooditems]
            row.append(r)
            
        return row   
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        all_foods = set()
        all_tables = set()
        mapping = defaultdict()
        for order in orders:
            _, table_id, food_name = order[0], int(order[1]), order[2]
            all_foods.add(food_name)
            all_tables.add(table_id)
            if table_id not in mapping:
                mapping[table_id] = defaultdict(int) 
            mapping[table_id][food_name] += 1
            
        all_foods = sorted(list(all_foods))
        all_tables = sorted(list(all_tables))
        labels = ['Table'] + all_foods
        data_table = [labels]
        for table in all_tables:
            temp = [str(table)]
            for food in all_foods:
                temp.append(str(mapping[table][food]))
            
            data_table.append(temp.copy())
            
        return data_table


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        
        #iterate through input list of lists
        tableFood = {}
        Fooditems = set()
        for i in orders:
            if i[1] in list(tableFood.keys()):
                tableFood[i[1]].append(i[2])
            else:
                tableFood[i[1]] = [i[2]]
            Fooditems.add(i[2])
            
        Fooditems = list(Fooditems)
        Fooditems.sort()
        
        outTable = []
        
        for k, v in list(tableFood.items()):
            tmp = [k]
            for i in Fooditems:
                tmp.append(str(v.count(i)))
            outTable.append(tmp)
        
        outTable.sort(key = lambda outTable: int(outTable[0]))
        # print([['Table'] + Fooditems] + outTable)
        
        return [['Table'] + Fooditems] + outTable
        
        # test_list.sort(key = lambda test_list: test_list[1]) 


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        foods = {}
        tables = {}
        nos = set()
        for order in orders:
            nos.add(order[1])
            foods[order[2]] = 0
            
        for order in orders:
            if order[1] not in tables:
                tables[order[1]] = foods.copy()
            tables[order[1]][order[2]] += 1
        titles = [\"Table\"]
        foodkeys = sorted(foods.keys())
        for key in foodkeys:
            titles.append(key)
            
        nos = list(nos)
        nos = list(map(int,nos))
        nos = sorted(nos)
        print(nos)
        ans = []
        ans.append(titles)
        for i in nos:
            tmp = [str(i)]
            
            for key in foodkeys:
                tmp.append(str(tables[str(i)][key]))
            ans.append(tmp)
            
        return ans
        
        
            

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d = {}
        dn = []
        for o in orders:
            if o[2] not in dn:
                dn.append(o[2])
                
            if o[1] in d:
                if o[2] in d[o[1]]:
                    d[o[1]][o[2]] += 1
                else:
                    d[o[1]][o[2]] = 1
            else:
                d[o[1]] = {}
                d[o[1]][o[2]] = 1
             
        dn.sort()
        fr = ['Table']
        for n in dn:
            fr.append(n)
        ans = [fr]
        st = list(d.keys())
        st.sort(key=lambda x:int(x))
        for t in st:
            r = [t]
            for n in dn:
                if n in d[t]:
                    r.append(str(d[t][n]))
                else:
                    r.append('0')
            ans.append(r)
        
        return ans
                


from collections import defaultdict

class Solution:
    
    def tables_to_lists(self, tables):
        for table in tables:
            temp = tables[table]
            temp[0] = int(table)
            yield temp
            
    
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        all_foods = sorted(list(set(map(lambda x: x[2], orders))))
        all_foods.insert(0, \"Table\")
        
        result = []
        result.append(all_foods)
        
        
        tables = defaultdict(lambda: [0] * (len(all_foods)) )
        
        for order in orders:
            name, table, item = order
            tables[table][all_foods.index(item)] += 1
          
        subresult = [t for t in self.tables_to_lists(tables)]
        
        result.extend(sorted(subresult, key=lambda x: x[0]))
        
        for i in range(1, len(result)):
            result[i] = list(map(str, result[i]))
        
        return result
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d =collections.defaultdict(lambda: collections.defaultdict(int))
        foods = set()
        for _, table, food in orders:
            foods.add(food)
            d[int(table)][food]+=1
        
        res = [[0]*(len(foods)+1) for _ in range(len(list(d.keys()))+1)]
        columns = {food: i+1 for i, food in enumerate(sorted(foods))}
        res[0] = ['Table']+sorted(foods)
        for i,table in enumerate(sorted(d.keys())):
            res[i+1][0]=table
            for food in d[table]:
                res[i+1][columns[food]]+=d[table][food]
        return [list(map(str, x)) for x in res]


class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        order = {}
        uniquefood = set()
        for o in orders:
            tableno = o[1]
            item = o[2]
            uniquefood.add(item)
            if tableno not in order:
                order[tableno] = {item:1}
            else:
                if item not in order[tableno]:
                    order[tableno][item] = 1
                else:
                    order[tableno][item] += 1
        header = [\"Table\"]
        for i in sorted(uniquefood):
            header.append(i)
        res = [header]
        for o in sorted(order, key = lambda x:int(x)):
            temp = []
            temp.append(o)
            for i in header[1:]:
                if i in order[o]:
                    temp.append(str(order[o][i]))
                else:
                    temp.append(\"0\")
            res.append(temp)
        return res
        

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        d = dict()
        food_items = set()
        
        for _, table, food in orders:
            table = int(table)
            if table not in d:
                d[table] = collections.Counter()
            d[table][food] += 1
            food_items.add(food)
        
        food_items = sorted(food_items)
        cols = ['Table'] + food_items
        
        
        out = [cols]
        for table in sorted(d.keys()):
            
            l = [str(table)]
            for f in food_items:
                if f not in d[table]:
                    l.append(\"0\")
                else:
                    l.append(str(d[table][f]))
            out.append(l)
        return out