-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterview5.java
More file actions
54 lines (47 loc) · 1.38 KB
/
Interview5.java
File metadata and controls
54 lines (47 loc) · 1.38 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
package pramp;
import java.util.HashMap;
import java.util.Map;
/**
* Split the words in a given string and print all of them in the descending
* order of their occurences count.
*
* @author: shivam.maharshi
*/
class Interview5 {
// 1. Parse the doc and figure out words split by space, punctuations or both.
// 2. Store them in a HasHMap and keep occurrences count.
// 3. Traverse HashMap keyset and sort it.
// 4. Time: O(nLog(n)): where n are the words. Space: O(n)
public static void getCount(String s) {
if (s == null || s.length() == 0)
return;
// This returns only words as array from s.
// String[] words = s.split("\\W+");
Map<String, Integer> m = new HashMap<>();
s = s.toLowerCase();
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < s.length()) {
char c = s.charAt(i);
if (isWordChar(c))
sb.append(c);
else if (sb.length() == 0)
addWord(m, sb);
i++;
}
if (sb.length() == 0)
addWord(m, sb);
System.out.println(m.toString());
}
public static boolean isWordChar(char c) {
return (c <= '9' && c >= '0') || (c <= 'z' && c >= 'a');
}
public static void addWord(Map<String, Integer> m, StringBuilder sb) {
String word = sb.toString();
if (m.containsKey(word))
m.put(word, m.get(word) + 1);
else
m.put(word, 1);
sb = new StringBuilder();
}
}