Leetcode 2284 Sender With Largest Word Count Solution in java | Hindi Coding Community

0

 


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".




class Solution {
public String largestWordCount(String[] messages, String[] senders) {
Map<String,Integer> m=new TreeMap<>();
int n=messages.length;
for(int i=0;i<n;i++){
String a[]=messages[i].split(" ");
if(m.containsKey(senders[i])){
m.put(senders[i],m.get(senders[i])+a.length);
}else{
m.put(senders[i],a.length);
}
}
int max=Collections.max(m.values());
String ans="";
for(Map.Entry k : m.entrySet()){
int x= (int)k.getValue();
if(x==max){
ans=String.valueOf(k.getKey());
}
}
return ans;
}
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !