BruteForce to Optimal Solution 🔥 | Explained 💯 [C++]
1. Brute Force Solution [Accepted \u2705]\n### Approach ->\nwe have taken a HashMap to store the count of words (in a message, counted using stringstream) of ea
## 1. Brute Force Solution [Accepted \u2705]\n### Approach ->\nwe have taken a **HashMap** to store the **count of words** (*in a message, counted using `stringstream`*) of each sender.\n\nand as we were inserting values, **we kept track of max frequency** at that point of time using `mx = max(mx, freq[senders[i]]);`\n\nAt last, just do **deal with equal frequencies**, we created a `set<string>` to get sender names in **Lexicogrphically Sorted Order** (Last string in the set will be Lexicographically largest).\n\n\n\n```\nstring largestWordCount(vector<string>& messages, vector<string>& senders) {\n    int n = senders.size();\n\n    unordered_map<string, int> freq;\n    int mx = INT_MIN;\n\n    for(int i = 0; i < n; ++i) {\n        int cnt = 0;\n\n        stringstream ss(messages[i]);\n        string word;\n\n        while(ss >> word)\n            ++cnt;\n\n        freq[senders[i]] += cnt;\n        mx = max(mx, freq[senders[i]]);\n    }\n\n\n    set<string> st;\n\n    for(auto &it : freq)\n        if(it.second == mx)\n            st.insert(it.first);\n\n    return *st.rbegin();\n}\n```\n## 2. Optimised [Accepted  \u2705]\n### Approach ->\nwe are doing kind of similar things here too, but in an **Optimised Way!** \uD83D\uDD25\n\nwe are storing the count of words (in a message, *counted using white spaces* `\' \'`) of each sender and at the same time **Comparing the max frequency** and as well as **strings** (in case of *same frequency*, to find **Lexicographically Largest**)\n```\nstring largestWordCount(vector<string>& messages, vector<string>& senders) {\n    int n = senders.size();\n    string maxString = "";\n    int maxMsgs = 0;\n\n    unordered_map<string, int> count;\n\n    for(int i = 0; i < n; ++i) {\n        int words = 1; // total spaces + 1\n        string msg =  messages[i];\n        string sender = senders[i];\n\n        for(const char &c : msg)\n            if(c == \' \')\n                ++words;\n\n        count[sender] += words;\n\n        if((count[sender] > maxMsgs)|| (count[sender] == maxMsgs && sender > maxString)) {\n            maxString = sender;\n            maxMsgs = count[sender];\n        }\n    }\n\n    return maxString;\n}\n```\n\nI Hope you found it **Helpful** \uD83D\uDCA1 , and if you have *Any Doubts*, **Please ask in Comments** \uD83D\uDCAC\n### Kindly UpVote \uD83D\uDD3C, it Really Motivates :) \uD83D\uDD25
['String', 'C', 'C++']