C++ Beginner Friendly Approach. Explained in detail.
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\nThe problem is asking us to return the display table vector such that it should dis
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\nThe problem is asking us to return the display table vector such that it should display the table number and the count of each food item that was ordered from that table. To return such a table we can use set, map, unordered map and vector.\r\n\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\n**1.** The first task is to get all the unique food items from the order-vector to create a table header. And when there\'s something unique to be extracted from the code the first thing that comes in mind is set. So, lets create one and call it foodItems for now std::set<std::string> foodItems.\r\n\r\n**2.** Secondly, we need to get all the table numbers, foodItems and their counts that has been orderd from that particular table. \r\n    - To do so we can make an map<>. since the table number has to be in ascending order. \r\n    - std::map<int, {see the below sentence}>. since the table number is an integer value.\r\n    - std::map<int, std::unordered_map<std::string, {see the below sentence}>>. since we want to create a pair of foodItem and their counts. Instead of unorderd_map we can use map also.\r\n    - map<int, unordered_map<std::string, int>. since the food count will be an integer value.\r\n\r\n*In std::map<int, std::unordered_map<std::string, int>> the int from map represents the table number that will be in ascending order. the unorderd_map within the map represents the foodItems and their cound. So, let\'s call it tableNumbersAndFoodItemsCount.*\r\n\r\n**3.** Store unique foodItems in foodItem set. And table number foodItems and their counts in std::map<int std::unordered_map<std::string, int>>.\r\n\r\n**4.** Create a tableHeader vector with "Table" as initial value and the add all the unique food items from above set.\r\n\r\n**5.** Initialize a 2D ans vector with first block as tableHeader in it.\r\n\r\n**6.** Loop through tableNumbersAndFoodItemsCount.\r\n\r\n***In loop***\r\n\r\n**7.** Create a std::vector<std::string> temp vector within tableNumbersAndFoodItemsCount as the same size of tableHeader.\r\n\r\n**8.** Push tabel number as first value in temp vector.\r\n\r\n**9.** Get the food item index position from tableHeader vector so that the counter can be placed on that index.\r\n\r\n**10.** Once the inner loop is done push the temp vector into ans vector.\r\n\r\n**11.** return ans vector.\r\n\r\n\r\n# Complexity\r\n- Time complexity: \r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\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\npublic:\r\n    vector<vector<string>> displayTable(vector<vector<string>>& orders) {\r\n        std::map<int, std::unordered_map<std::string, int>>\r\n            tableNumbersAndFoodItemsCount;\r\n        std::set<std::string> foodItems;\r\n\r\n        for (int i{0}; i < orders.size(); i++) {\r\n            foodItems.insert(orders[i][2]);\r\n            tableNumbersAndFoodItemsCount[std::stoi(orders[i][1])]\r\n                                         [orders[i][2]]++;\r\n        }\r\n\r\n        // Initialize a table header.\r\n        std::vector<std::string> tableHeader{"Table"};\r\n        for (auto i : foodItems)\r\n            tableHeader.push_back(i);\r\n\r\n        std::vector<std::vector<std::string>> ans{tableHeader};\r\n\r\n        for (auto i : tableNumbersAndFoodItemsCount) {\r\n            int n = foodItems.size() + 1;\r\n            std::vector<std::string> temp(n);\r\n            std::fill(temp.begin(), temp.end(), std::to_string(0));\r\n            temp[0] = std::to_string(i.first);\r\n            for (auto j : i.second) {\r\n                int pos =\r\n                    std::find(tableHeader.begin(), tableHeader.end(), j.first) -\r\n                    tableHeader.begin();\r\n                temp[pos] = std::to_string(j.second);\r\n            }\r\n            ans.push_back(temp);\r\n        }\r\n\r\n        return ans;\r\n    }\r\n};\r\n```\r\n\r\n![upvote.jpeg](https://assets.leetcode.com/users/images/544b35f3-d27c-42f5-a1d7-7c3513be1691_1713037412.3910432.jpeg)\r\n\r\n\r\n
['Array', 'Hash Table', 'String', 'Sorting', 'Ordered Set', 'C++']