返回介绍

solution / 2200-2299 / 2284.Sender With Largest Word Count / README_EN

发布于 2024-06-17 01:03:07 字数 5340 浏览 0 评论 0 收藏 0

2284. Sender With Largest Word Count

中文文档

Description

You have a chat log of n messages. You are given two string arrays messages and senders where messages[i] is a message sent by senders[i].

A message is list of words that are separated by a single space with no leading or trailing spaces. The word count of a sender is the total number of words sent by the sender. Note that a sender may send more than one message.

Return _the sender with the largest word count_. If there is more than one sender with the largest word count, return _the one with the lexicographically largest name_.

Note:

  • Uppercase letters come before lowercase letters in lexicographical order.
  • "Alice" and "alice" are distinct.

 

Example 1:

Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"]
Output: "Alice"
Explanation: Alice sends a total of 2 + 3 = 5 words.
userTwo sends a total of 2 words.
userThree sends a total of 3 words.
Since Alice has the largest word count, we return "Alice".

Example 2:

Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
Output: "Charlie"
Explanation: Bob sends a total of 5 words.
Charlie sends a total of 5 words.
Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie.

 

Constraints:

  • n == messages.length == senders.length
  • 1 <= n <= 104
  • 1 <= messages[i].length <= 100
  • 1 <= senders[i].length <= 10
  • messages[i] consists of uppercase and lowercase English letters and ' '.
  • All the words in messages[i] are separated by a single space.
  • messages[i] does not have leading or trailing spaces.
  • senders[i] consists of uppercase and lowercase English letters only.

Solutions

Solution 1

class Solution:
  def largestWordCount(self, messages: List[str], senders: List[str]) -> str:
    cnt = Counter()
    for msg, sender in zip(messages, senders):
      cnt[sender] += msg.count(' ') + 1
    ans = ''
    for sender, v in cnt.items():
      if cnt[ans] < v or (cnt[ans] == v and ans < sender):
        ans = sender
    return ans
class Solution {
  public String largestWordCount(String[] messages, String[] senders) {
    Map<String, Integer> cnt = new HashMap<>();
    int n = senders.length;
    for (int i = 0; i < n; ++i) {
      int v = 1;
      for (int j = 0; j < messages[i].length(); ++j) {
        if (messages[i].charAt(j) == ' ') {
          ++v;
        }
      }
      cnt.merge(senders[i], v, Integer::sum);
    }
    String ans = senders[0];
    for (var e : cnt.entrySet()) {
      String sender = e.getKey();
      if (cnt.get(ans) < cnt.get(sender)
        || (cnt.get(ans) == cnt.get(sender) && ans.compareTo(sender) < 0)) {
        ans = sender;
      }
    }
    return ans;
  }
}
class Solution {
public:
  string largestWordCount(vector<string>& messages, vector<string>& senders) {
    unordered_map<string, int> cnt;
    int n = senders.size();
    for (int i = 0; i < n; ++i) {
      int v = count(messages[i].begin(), messages[i].end(), ' ') + 1;
      cnt[senders[i]] += v;
    }
    string ans = senders[0];
    for (auto& [sender, v] : cnt) {
      if (cnt[ans] < v || (cnt[ans] == v && ans < sender)) {
        ans = sender;
      }
    }
    return ans;
  }
};
func largestWordCount(messages []string, senders []string) (ans string) {
  cnt := map[string]int{}
  for i, msg := range messages {
    v := strings.Count(msg, " ") + 1
    cnt[senders[i]] += v
  }
  for sender, v := range cnt {
    if cnt[ans] < v || (cnt[ans] == v && ans < sender) {
      ans = sender
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文