|
| 1 | +/** |
| 2 | + * [2284] Sender With Largest Word Count |
| 3 | + * |
| 4 | + * 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]. |
| 5 | + * 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. |
| 6 | + * 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. |
| 7 | + * Note: |
| 8 | + * |
| 9 | + * Uppercase letters come before lowercase letters in lexicographical order. |
| 10 | + * "Alice" and "alice" are distinct. |
| 11 | + * |
| 12 | + * |
| 13 | + * Example 1: |
| 14 | + * |
| 15 | + * Input: messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"], senders = ["Alice","userTwo","userThree","Alice"] |
| 16 | + * Output: "Alice" |
| 17 | + * Explanation: Alice sends a total of 2 + 3 = 5 words. |
| 18 | + * userTwo sends a total of 2 words. |
| 19 | + * userThree sends a total of 3 words. |
| 20 | + * Since Alice has the largest word count, we return "Alice". |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * |
| 24 | + * Input: messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"] |
| 25 | + * Output: "Charlie" |
| 26 | + * Explanation: Bob sends a total of 5 words. |
| 27 | + * Charlie sends a total of 5 words. |
| 28 | + * Since there is a tie for the largest word count, we return the sender with the lexicographically larger name, Charlie. |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * |
| 32 | + * n == messages.length == senders.length |
| 33 | + * 1 <= n <= 10^4 |
| 34 | + * 1 <= messages[i].length <= 100 |
| 35 | + * 1 <= senders[i].length <= 10 |
| 36 | + * messages[i] consists of uppercase and lowercase English letters and ' '. |
| 37 | + * All the words in messages[i] are separated by a single space. |
| 38 | + * messages[i] does not have leading or trailing spaces. |
| 39 | + * senders[i] consists of uppercase and lowercase English letters only. |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/sender-with-largest-word-count/ |
| 45 | +// discuss: https://leetcode.com/problems/sender-with-largest-word-count/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + // Credit: https://leetcode.com/problems/sender-with-largest-word-count/solutions/5300813/two-line-solution-beats-88-at-speed-hash-gr14/ |
| 51 | + pub fn largest_word_count(messages: Vec<String>, senders: Vec<String>) -> String { |
| 52 | + let generate_map = || { |
| 53 | + let mut map = std::collections::HashMap::new(); |
| 54 | + map.insert(String::new(), 0); |
| 55 | + map |
| 56 | + }; |
| 57 | + |
| 58 | + senders |
| 59 | + .into_iter() |
| 60 | + .zip(messages.into_iter()) |
| 61 | + .fold( |
| 62 | + (String::new(), generate_map()), |
| 63 | + |(max_sender, mut map), (sender, message)| { |
| 64 | + *map.entry(sender.clone()).or_insert(0) += message.split(" ").count() as i32; |
| 65 | + let (cmp1, cmp2) = ( |
| 66 | + map[&sender] > map[&max_sender], |
| 67 | + map[&sender] == map[&max_sender], |
| 68 | + ); |
| 69 | + if cmp1 || cmp2 && sender > max_sender { |
| 70 | + (sender, map) |
| 71 | + } else { |
| 72 | + (max_sender, map) |
| 73 | + } |
| 74 | + }, |
| 75 | + ) |
| 76 | + .0 |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// submission codes end |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_2284_example_1() { |
| 88 | + let messages = vec_string![ |
| 89 | + "Hello userTwooo", |
| 90 | + "Hi userThree", |
| 91 | + "Wonderful day Alice", |
| 92 | + "Nice day userThree" |
| 93 | + ]; |
| 94 | + let senders = vec_string!["Alice", "userTwo", "userThree", "Alice"]; |
| 95 | + |
| 96 | + let result = "Alice".to_string(); |
| 97 | + |
| 98 | + assert_eq!(Solution::largest_word_count(messages, senders), result); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_2284_example_2() { |
| 103 | + let messages = vec_string![ |
| 104 | + "How is leetcode for everyone", |
| 105 | + "Leetcode is useful for practice" |
| 106 | + ]; |
| 107 | + let senders = vec_string!["Bob", "Charlie"]; |
| 108 | + |
| 109 | + let result = "Charlie".to_string(); |
| 110 | + |
| 111 | + assert_eq!(Solution::largest_word_count(messages, senders), result); |
| 112 | + } |
| 113 | +} |
0 commit comments