-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommonWords.java
executable file
·66 lines (56 loc) · 1.54 KB
/
CommonWords.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.vee.algorithms.interview;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
/**
* Print most common words in a sentence or a group of sentences.
* It should be case sensitive. If they are more than 1 then print all of them
*
*
*/
//interview = twitter
public class CommonWords {
public String[] common(String s) {
String a[] = s.split("\\W+");
Map<String, Integer> occurences = new LinkedHashMap<String, Integer>();
for (String str: a) {
Integer val = occurences.get(str);
if (val == null) {
occurences.put(str, 1);
} else {
occurences.put(str, val+1);
}
}
int max = -1;
List<String> list = new ArrayList<String>();
for (Entry<String, Integer> e : occurences.entrySet()) {
if (max < e.getValue()) {
list.clear();
max = e.getValue();
list.add(e.getKey());
} else if (max == e.getValue()) {
list.add(e.getKey());
}
}
return list.toArray(new String[0]);
}
@Test
public void testSingleWord() {
String s = "Print most common words in a sentence or a group of sentences.";
String a[] = new CommonWords().common(s);
assertEquals(a.length, 1);
assertEquals(a[0], "a");
}
@Test
public void testMultiWord() {
String s = "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo Buffalo";
String a[] = new CommonWords().common(s);
assertEquals(a.length, 2);
assertEquals(a[0], "Buffalo");
assertEquals(a[1], "buffalo");
}
}