-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathconversationlog.java
More file actions
64 lines (49 loc) · 1.3 KB
/
conversationlog.java
File metadata and controls
64 lines (49 loc) · 1.3 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
public class conversationlog {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int msgs = scan.nextInt();
scan.nextLine();
HashSet<String> allWords = new HashSet<>();
HashMap<String , Integer> wordCount = new HashMap<>();
HashMap<String , HashSet<String>> userVocab = new HashMap<>();
while (msgs --> 0)
{
StringTokenizer token = new StringTokenizer(scan.nextLine());
String user = token.nextToken();
if (!userVocab.containsKey(user))
userVocab.put(user, new HashSet<>());
while (token.hasMoreTokens())
{
String word = token.nextToken();
if (!wordCount.containsKey(word))
wordCount.put(word , 0);
else
wordCount.put(word, wordCount.get(word) + 1);
userVocab.get(user).add(word);
allWords.add(word);
}
}
for (String name : userVocab.keySet())
allWords.retainAll(userVocab.get(name));
String[] words = allWords.toArray(new String[0]);
if (words.length == 0)
System.out.println("ALL CLEAR");
else
{
Arrays.sort(words , new Comparator<String>() {
public int compare (String str1 , String str2) {
int a = wordCount.get(str1);
int b = wordCount.get(str2);
if (a == b)
return str1.compareTo(str2);
else
return b - a;
}
});
for (String word : words)
System.out.println(word);
}
scan.close();
}
}