diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README.md b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README.md index 54a2cb136451a..bdba152559ba2 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README.md +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README.md @@ -69,9 +69,9 @@ num[2] = '0' 。数字 2 在 num 中出现了 0 次。 ### 方法一:计数 + 枚举 -统计字符串中每个数字出现的次数,然后枚举每个数字,判断其出现的次数是否与其值相等,若都相等则返回 `true`,否则返回 `false`。 +我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 统计字符串 $\textit{num}$ 中每个数字出现的次数,然后再枚举字符串 $\textit{num}$ 中的每个数字,判断其出现的次数是否等于该数字本身。如果对于所有的数字都满足这个条件,那么返回 $\text{true}$,否则返回 $\text{false}$。 -时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 `num` 的长度,而 $C$ 是数字的个数。本题中 $C=10$。 +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串 $\textit{num}$ 的长度,而 $|\Sigma|$ 是数字的取值范围,即 $10$。 @@ -80,8 +80,8 @@ num[2] = '0' 。数字 2 在 num 中出现了 0 次。 ```python class Solution: def digitCount(self, num: str) -> bool: - cnt = Counter(num) - return all(cnt[str(i)] == int(v) for i, v in enumerate(num)) + cnt = Counter(int(x) for x in num) + return all(cnt[i] == int(x) for i, x in enumerate(num)) ``` #### Java @@ -95,7 +95,7 @@ class Solution { ++cnt[num.charAt(i) - '0']; } for (int i = 0; i < n; ++i) { - if (cnt[i] != num.charAt(i) - '0') { + if (num.charAt(i) - '0' != cnt[i]) { return false; } } @@ -132,8 +132,8 @@ func digitCount(num string) bool { for _, c := range num { cnt[c-'0']++ } - for i, v := range num { - if cnt[i] != int(v-'0') { + for i, c := range num { + if int(c-'0') != cnt[i] { return false } } @@ -145,15 +145,16 @@ func digitCount(num string) bool { ```ts function digitCount(num: string): boolean { - const n = num.length; - const count = new Array(10).fill(0); - for (let i = 0; i < n; i++) { - count[i] = Number(num[i]); - } + const cnt: number[] = Array(10).fill(0); for (const c of num) { - count[c]--; + ++cnt[+c]; + } + for (let i = 0; i < num.length; ++i) { + if (cnt[i] !== +num[i]) { + return false; + } } - return count.every(v => v === 0); + return true; } ``` @@ -162,16 +163,18 @@ function digitCount(num: string): boolean { ```rust impl Solution { pub fn digit_count(num: String) -> bool { - let s = num.as_bytes(); - let n = num.len(); - let mut count = [0; 10]; - for i in 0..n { - count[i] = s[i] - b'0'; + let mut cnt = vec![0; 10]; + for c in num.chars() { + let x = c.to_digit(10).unwrap() as usize; + cnt[x] += 1; } - for c in s { - count[(c - b'0') as usize] -= 1; + for (i, c) in num.chars().enumerate() { + let x = c.to_digit(10).unwrap() as usize; + if cnt[i] != x { + return false; + } } - count.iter().all(|v| *v == 0) + true } } ``` @@ -180,15 +183,12 @@ impl Solution { ```c bool digitCount(char* num) { - int count[10] = {0}; - for (int i = 0; num[i]; i++) { - count[i] = num[i] - '0'; - } - for (int i = 0; num[i]; i++) { - count[num[i] - '0']--; + int cnt[10] = {0}; + for (int i = 0; num[i] != '\0'; ++i) { + ++cnt[num[i] - '0']; } - for (int i = 0; i < 10; i++) { - if (count[i] != 0) { + for (int i = 0; num[i] != '\0'; ++i) { + if (cnt[i] != num[i] - '0') { return false; } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md index 28e6796c0d1db..bda13d68dedc4 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md @@ -65,7 +65,11 @@ The indices 0 and 1 both violate the condition, so return false. -### Solution 1 +### Solution 1: Counting + Enumeration + +We can use an array $\textit{cnt}$ of length $10$ to count the occurrences of each digit in the string $\textit{num}$. Then, we enumerate each digit in the string $\textit{num}$ and check if its occurrence count equals the digit itself. If this condition is satisfied for all digits, we return $\text{true}$; otherwise, we return $\text{false}$. + +The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $\textit{num}$, and $|\Sigma|$ is the range of possible digit values, which is $10$. @@ -74,8 +78,8 @@ The indices 0 and 1 both violate the condition, so return false. ```python class Solution: def digitCount(self, num: str) -> bool: - cnt = Counter(num) - return all(cnt[str(i)] == int(v) for i, v in enumerate(num)) + cnt = Counter(int(x) for x in num) + return all(cnt[i] == int(x) for i, x in enumerate(num)) ``` #### Java @@ -89,7 +93,7 @@ class Solution { ++cnt[num.charAt(i) - '0']; } for (int i = 0; i < n; ++i) { - if (cnt[i] != num.charAt(i) - '0') { + if (num.charAt(i) - '0' != cnt[i]) { return false; } } @@ -126,8 +130,8 @@ func digitCount(num string) bool { for _, c := range num { cnt[c-'0']++ } - for i, v := range num { - if cnt[i] != int(v-'0') { + for i, c := range num { + if int(c-'0') != cnt[i] { return false } } @@ -139,15 +143,16 @@ func digitCount(num string) bool { ```ts function digitCount(num: string): boolean { - const n = num.length; - const count = new Array(10).fill(0); - for (let i = 0; i < n; i++) { - count[i] = Number(num[i]); - } + const cnt: number[] = Array(10).fill(0); for (const c of num) { - count[c]--; + ++cnt[+c]; + } + for (let i = 0; i < num.length; ++i) { + if (cnt[i] !== +num[i]) { + return false; + } } - return count.every(v => v === 0); + return true; } ``` @@ -156,16 +161,18 @@ function digitCount(num: string): boolean { ```rust impl Solution { pub fn digit_count(num: String) -> bool { - let s = num.as_bytes(); - let n = num.len(); - let mut count = [0; 10]; - for i in 0..n { - count[i] = s[i] - b'0'; + let mut cnt = vec![0; 10]; + for c in num.chars() { + let x = c.to_digit(10).unwrap() as usize; + cnt[x] += 1; } - for c in s { - count[(c - b'0') as usize] -= 1; + for (i, c) in num.chars().enumerate() { + let x = c.to_digit(10).unwrap() as usize; + if cnt[i] != x { + return false; + } } - count.iter().all(|v| *v == 0) + true } } ``` @@ -174,15 +181,12 @@ impl Solution { ```c bool digitCount(char* num) { - int count[10] = {0}; - for (int i = 0; num[i]; i++) { - count[i] = num[i] - '0'; - } - for (int i = 0; num[i]; i++) { - count[num[i] - '0']--; + int cnt[10] = {0}; + for (int i = 0; num[i] != '\0'; ++i) { + ++cnt[num[i] - '0']; } - for (int i = 0; i < 10; i++) { - if (count[i] != 0) { + for (int i = 0; num[i] != '\0'; ++i) { + if (cnt[i] != num[i] - '0') { return false; } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.c b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.c index 1eaaa0c1c7129..8d8993c379289 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.c +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.c @@ -1,13 +1,10 @@ bool digitCount(char* num) { - int count[10] = {0}; - for (int i = 0; num[i]; i++) { - count[i] = num[i] - '0'; + int cnt[10] = {0}; + for (int i = 0; num[i] != '\0'; ++i) { + ++cnt[num[i] - '0']; } - for (int i = 0; num[i]; i++) { - count[num[i] - '0']--; - } - for (int i = 0; i < 10; i++) { - if (count[i] != 0) { + for (int i = 0; num[i] != '\0'; ++i) { + if (cnt[i] != num[i] - '0') { return false; } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.go b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.go index c0c28be166b2d..abb22e8f7a4f2 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.go +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.go @@ -3,8 +3,8 @@ func digitCount(num string) bool { for _, c := range num { cnt[c-'0']++ } - for i, v := range num { - if cnt[i] != int(v-'0') { + for i, c := range num { + if int(c-'0') != cnt[i] { return false } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.java b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.java index a0be9313ccc25..c00aef7ccdcae 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.java +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.java @@ -6,7 +6,7 @@ public boolean digitCount(String num) { ++cnt[num.charAt(i) - '0']; } for (int i = 0; i < n; ++i) { - if (cnt[i] != num.charAt(i) - '0') { + if (num.charAt(i) - '0' != cnt[i]) { return false; } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.py b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.py index e45e6a98dc364..9ee307c6e7c41 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.py +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.py @@ -1,4 +1,4 @@ class Solution: def digitCount(self, num: str) -> bool: - cnt = Counter(num) - return all(cnt[str(i)] == int(v) for i, v in enumerate(num)) + cnt = Counter(int(x) for x in num) + return all(cnt[i] == int(x) for i, x in enumerate(num)) diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.rs b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.rs index a67463dc882e7..ef4c68f936e27 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.rs +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.rs @@ -1,14 +1,16 @@ impl Solution { pub fn digit_count(num: String) -> bool { - let s = num.as_bytes(); - let n = num.len(); - let mut count = [0; 10]; - for i in 0..n { - count[i] = s[i] - b'0'; + let mut cnt = vec![0; 10]; + for c in num.chars() { + let x = c.to_digit(10).unwrap() as usize; + cnt[x] += 1; } - for c in s { - count[(c - b'0') as usize] -= 1; + for (i, c) in num.chars().enumerate() { + let x = c.to_digit(10).unwrap() as usize; + if cnt[i] != x { + return false; + } } - count.iter().all(|v| *v == 0) + true } } diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.ts b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.ts index 0b2d74eaf97a0..4771e2d7da88b 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.ts +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.ts @@ -1,11 +1,12 @@ function digitCount(num: string): boolean { - const n = num.length; - const count = new Array(10).fill(0); - for (let i = 0; i < n; i++) { - count[i] = Number(num[i]); - } + const cnt: number[] = Array(10).fill(0); for (const c of num) { - count[c]--; + ++cnt[+c]; + } + for (let i = 0; i < num.length; ++i) { + if (cnt[i] !== +num[i]) { + return false; + } } - return count.every(v => v === 0); + return true; } diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/README.md b/solution/2200-2299/2284.Sender With Largest Word Count/README.md index 1c4543072a1c2..4d5e3d7921cdf 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/README.md +++ b/solution/2200-2299/2284.Sender With Largest Word Count/README.md @@ -77,9 +77,9 @@ Charlie 总共发出了 5 个单词。 ### 方法一:哈希表 + 枚举 -我们用哈希表 `cnt` 统计每个发件人的单词数,然后枚举每个发件人,找到单词数最多且字典序最大的发件人即可。 +我们可以用一个哈希表 $\textit{cnt}$ 记录每个发件人的单词数,然后遍历哈希表找到单词数最多的发件人,如果有多个发件人发出最多单词数,我们返回字典序最大的名字。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `messages` 的长度。 +时间复杂度 $O(n + L)$,空间复杂度 $O(n)$,其中 $n$ 是消息的数量,而 $L$ 是所有消息的总长度。 @@ -89,12 +89,12 @@ Charlie 总共发出了 5 个单词。 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 + for message, sender in zip(messages, senders): + cnt[sender] += message.count(" ") + 1 + ans = senders[0] + for k, v in cnt.items(): + if cnt[ans] < v or (cnt[ans] == v and ans < k): + ans = k return ans ``` @@ -103,9 +103,8 @@ class Solution: ```java class Solution { public String largestWordCount(String[] messages, String[] senders) { - Map cnt = new HashMap<>(); - int n = senders.length; - for (int i = 0; i < n; ++i) { + Map cnt = new HashMap<>(senders.length); + for (int i = 0; i < messages.length; ++i) { int v = 1; for (int j = 0; j < messages[i].length(); ++j) { if (messages[i].charAt(j) == ' ') { @@ -116,10 +115,10 @@ class Solution { } 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; + String k = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && ans.compareTo(k) < 0)) { + ans = k; } } return ans; @@ -134,15 +133,14 @@ class Solution { public: string largestWordCount(vector& messages, vector& senders) { unordered_map cnt; - int n = senders.size(); - for (int i = 0; i < n; ++i) { + for (int i = 0; i < messages.size(); ++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; + for (auto& [k, v] : cnt) { + if (cnt[ans] < v || (cnt[ans] == v && ans < k)) { + ans = k; } } return ans; @@ -153,18 +151,42 @@ public: #### Go ```go -func largestWordCount(messages []string, senders []string) (ans string) { - cnt := map[string]int{} - for i, msg := range messages { - v := strings.Count(msg, " ") + 1 +func largestWordCount(messages []string, senders []string) string { + cnt := make(map[string]int) + for i, message := range messages { + v := strings.Count(message, " ") + 1 cnt[senders[i]] += v } - for sender, v := range cnt { - if cnt[ans] < v || (cnt[ans] == v && ans < sender) { - ans = sender + + ans := senders[0] + for k, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && ans < k) { + ans = k } } - return + return ans +} +``` + +#### TypeScript + +```ts +function largestWordCount(messages: string[], senders: string[]): string { + const cnt: { [key: string]: number } = {}; + + for (let i = 0; i < messages.length; ++i) { + const v = messages[i].split(' ').length; + cnt[senders[i]] = (cnt[senders[i]] || 0) + v; + } + + let ans = senders[0]; + for (const k in cnt) { + if (cnt[ans] < cnt[k] || (cnt[ans] === cnt[k] && ans < k)) { + ans = k; + } + } + + return ans; } ``` diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md b/solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md index bc3c4199d8497..7610f2edae0b4 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md +++ b/solution/2200-2299/2284.Sender With Largest Word Count/README_EN.md @@ -75,7 +75,11 @@ Since there is a tie for the largest word count, we return the sender with the l -### Solution 1 +### Solution 1: Hash Table + Enumeration + +We can use a hash table $\textit{cnt}$ to record the word count for each sender. Then, we traverse the hash table to find the sender with the highest word count. If there are multiple senders with the highest word count, we return the name that is lexicographically largest. + +The time complexity is $O(n + L)$, and the space complexity is $O(n)$, where $n$ is the number of messages and $L$ is the total length of all messages. @@ -85,12 +89,12 @@ Since there is a tie for the largest word count, we return the sender with the l 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 + for message, sender in zip(messages, senders): + cnt[sender] += message.count(" ") + 1 + ans = senders[0] + for k, v in cnt.items(): + if cnt[ans] < v or (cnt[ans] == v and ans < k): + ans = k return ans ``` @@ -99,9 +103,8 @@ class Solution: ```java class Solution { public String largestWordCount(String[] messages, String[] senders) { - Map cnt = new HashMap<>(); - int n = senders.length; - for (int i = 0; i < n; ++i) { + Map cnt = new HashMap<>(senders.length); + for (int i = 0; i < messages.length; ++i) { int v = 1; for (int j = 0; j < messages[i].length(); ++j) { if (messages[i].charAt(j) == ' ') { @@ -112,10 +115,10 @@ class Solution { } 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; + String k = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && ans.compareTo(k) < 0)) { + ans = k; } } return ans; @@ -130,15 +133,14 @@ class Solution { public: string largestWordCount(vector& messages, vector& senders) { unordered_map cnt; - int n = senders.size(); - for (int i = 0; i < n; ++i) { + for (int i = 0; i < messages.size(); ++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; + for (auto& [k, v] : cnt) { + if (cnt[ans] < v || (cnt[ans] == v && ans < k)) { + ans = k; } } return ans; @@ -149,18 +151,42 @@ public: #### Go ```go -func largestWordCount(messages []string, senders []string) (ans string) { - cnt := map[string]int{} - for i, msg := range messages { - v := strings.Count(msg, " ") + 1 +func largestWordCount(messages []string, senders []string) string { + cnt := make(map[string]int) + for i, message := range messages { + v := strings.Count(message, " ") + 1 cnt[senders[i]] += v } - for sender, v := range cnt { - if cnt[ans] < v || (cnt[ans] == v && ans < sender) { - ans = sender + + ans := senders[0] + for k, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && ans < k) { + ans = k } } - return + return ans +} +``` + +#### TypeScript + +```ts +function largestWordCount(messages: string[], senders: string[]): string { + const cnt: { [key: string]: number } = {}; + + for (let i = 0; i < messages.length; ++i) { + const v = messages[i].split(' ').length; + cnt[senders[i]] = (cnt[senders[i]] || 0) + v; + } + + let ans = senders[0]; + for (const k in cnt) { + if (cnt[ans] < cnt[k] || (cnt[ans] === cnt[k] && ans < k)) { + ans = k; + } + } + + return ans; } ``` diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp index b6fca900db877..81a2fcfff0bf0 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp @@ -2,15 +2,14 @@ class Solution { public: string largestWordCount(vector& messages, vector& senders) { unordered_map cnt; - int n = senders.size(); - for (int i = 0; i < n; ++i) { + for (int i = 0; i < messages.size(); ++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; + for (auto& [k, v] : cnt) { + if (cnt[ans] < v || (cnt[ans] == v && ans < k)) { + ans = k; } } return ans; diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.go b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.go index 3390d6bcaa845..c174ee24cb14b 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.go +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.go @@ -1,13 +1,15 @@ -func largestWordCount(messages []string, senders []string) (ans string) { - cnt := map[string]int{} - for i, msg := range messages { - v := strings.Count(msg, " ") + 1 +func largestWordCount(messages []string, senders []string) string { + cnt := make(map[string]int) + for i, message := range messages { + v := strings.Count(message, " ") + 1 cnt[senders[i]] += v } - for sender, v := range cnt { - if cnt[ans] < v || (cnt[ans] == v && ans < sender) { - ans = sender + + ans := senders[0] + for k, v := range cnt { + if cnt[ans] < v || (cnt[ans] == v && ans < k) { + ans = k } } - return + return ans } \ No newline at end of file diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.java b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.java index 1bcbd1cfc1d2c..550e5ceff4803 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.java +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.java @@ -1,8 +1,7 @@ class Solution { public String largestWordCount(String[] messages, String[] senders) { - Map cnt = new HashMap<>(); - int n = senders.length; - for (int i = 0; i < n; ++i) { + Map cnt = new HashMap<>(senders.length); + for (int i = 0; i < messages.length; ++i) { int v = 1; for (int j = 0; j < messages[i].length(); ++j) { if (messages[i].charAt(j) == ' ') { @@ -13,10 +12,10 @@ public String largestWordCount(String[] messages, String[] senders) { } 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; + String k = e.getKey(); + int v = e.getValue(); + if (cnt.get(ans) < v || (cnt.get(ans) == v && ans.compareTo(k) < 0)) { + ans = k; } } return ans; diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.py b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.py index ec1a17e5b2381..d3dbfc2b61a83 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.py +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.py @@ -1,10 +1,10 @@ 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 + for message, sender in zip(messages, senders): + cnt[sender] += message.count(" ") + 1 + ans = senders[0] + for k, v in cnt.items(): + if cnt[ans] < v or (cnt[ans] == v and ans < k): + ans = k return ans diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.ts b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.ts new file mode 100644 index 0000000000000..4c5330bc10bad --- /dev/null +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.ts @@ -0,0 +1,17 @@ +function largestWordCount(messages: string[], senders: string[]): string { + const cnt: { [key: string]: number } = {}; + + for (let i = 0; i < messages.length; ++i) { + const v = messages[i].split(' ').length; + cnt[senders[i]] = (cnt[senders[i]] || 0) + v; + } + + let ans = senders[0]; + for (const k in cnt) { + if (cnt[ans] < cnt[k] || (cnt[ans] === cnt[k] && ans < k)) { + ans = k; + } + } + + return ans; +}