Java | HashMap | O(n) | Simple
Intuition\n Describe your first thoughts on how to solve this problem. \nCalculate the word count for every message. Maintain a hash map of senders -> word coun
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCalculate the word count for every message. Maintain a hash map of senders -> word count. Iterate over the hash map and figure out the sender with the largest word count.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n  public String largestWordCount(String[] messages, String[] senders) {\n    HashMap<String, Integer> map = new HashMap<>();\n\n    for (int i=0; i < messages.length; i++) {\n      int cnt = 0;\n      for (int j=0; j < messages[i].length(); j++)\n        if (messages[i].charAt(j) == \' \')\n          cnt++;\n\n      map.put(senders[i], map.getOrDefault(senders[i], 0) + cnt + 1);\n    }\n    String ans = "";\n    int maxWordCount = Integer.MIN_VALUE;\n    \n    for (Map.Entry<String, Integer> entry : map.entrySet()) {\n      String sender = map.getKey();\n      int wordCount = map.getValue();\n\n      if (wordCount >= maxWordCount) {\n        ans = \n          wordCount == maxWordCount\n            ? ans.compareTo(sender) > 0 ? ans : sender\n            : sender;\n        maxWordCount = wordCount;\n      }\n    }\n    return ans;\n  }\n}\n```
['Hash Table', 'Java']