JAVA | Clean solution using HashMap | Explained ✅
Solution explained using comments \uD83D\uDE07\n---\n### Code:\n\nclass Solution {\n    public String largestWordCount(String[] messages, String[] senders) {\n 
# Solution explained using comments \uD83D\uDE07\n---\n### Code:\n```\nclass Solution {\n    public String largestWordCount(String[] messages, String[] senders) {\n        // In a hashmap, record whhich person has sent how many words\n        Map<String, Integer> map = new HashMap<>();\n        for (int i = 0; i < messages.length; i++) {\n            // doing this would get us the number of words in that particular message\n            int words = messages[i].split(" ").length;\n            String name = senders[i];\n            // update in the map\n            map.put(name, words + map.getOrDefault(name, 0));\n        }\n\n        // a string \'ans\' to record our final sender\'s name\n        String ans = "";\n        // variable to keep track of the maximum number of words spoken by a sender\n        int max = 0;    \n        // go through the senders\n        for (String name : map.keySet()) {\n            // number of words current sender has sent\n            int words = map.get(name);  \n            // if number of words > max spoken words\n            if (words > max) {\n                max = words;    // update max\n                ans = name;     // make this sender our candidate\n            } \n            // if we have a tie in the max number of words spoken\n            else if (words == max) {\n                // keep the name which is lexicographically greater\n                int x = ans.compareTo(name);\n                ans = (x > 0)? ans : name;\n            }\n        }\n\n        // return the final sender\'s name\n        return ans;\n    }\n}\n```\n---\n#### Clean solution:\n```\nclass Solution {\n    public String largestWordCount(String[] messages, String[] senders) {\n        Map<String, Integer> map = new HashMap<>();\n        for (int i = 0; i < messages.length; i++) {\n            int words = messages[i].split(" ").length;\n            String name = senders[i];\n            map.put(name, words + map.getOrDefault(name, 0));\n        }\n\n        String ans = "";\n        int max = 0;    \n        for (String name : map.keySet()) {\n            int words = map.get(name);  \n            if (words > max) {\n                max = words;    \n                ans = name;     \n            } else if (words == max) {\n                int x = ans.compareTo(name);\n                ans = (x > 0)? ans : name;\n            }\n        }\n\n        return ans;\n    }\n}\n```\n---\n### Complexity analysis:\n##### Time complexity: $$O(n) + O(n) => O(n)$$\n> A $$O(n)$$ to populate our map. Another $$O(n)$$ to go thorugh the map to go through the map.\n\n##### Space complexity: $$O(n)$$\n> We use a hashmap to store the data of our senders.\n---\n                          Do comment if you have any doubt \uD83D\uDC4D\uD83C\uDFFB\n                                 Upvote if you like \uD83D\uDC96
['Array', 'Hash Table', 'String', 'Java']