-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSender With Largest Word Count.java
46 lines (38 loc) · 1.11 KB
/
Sender With Largest Word Count.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Runtime: 26 ms (Top 92.47%) | Memory: 48.80 MB (Top 99.46%)
class Solution {
public String largestWordCount(String[] messages, String[] senders)
{
HashMap<String,Integer> map = new HashMap<>();
String res = "";int max =0;
for(int i=0; i<messages.length;i++)
{
int words = get_count(messages[i]);
if(!map.containsKey(senders[i]))
map.put(senders[i] , words);
else
map.put(senders[i],map.get(senders[i]) + words);
}
for(String s: map.keySet())
{
if(map.get(s) > max)
{
res = s;
max = map.get(s);
}
if(map.get(s) == max && res.compareTo(s) < 0)
res = s;
}
return res;
}
private int get_count(String s)
{
int spaces = 0;
for(int i=0; i<s.length();i++)
{
char ch = s.charAt(i);
if(ch == ' ')
spaces++;
}
return spaces+1;
}
}