forked from xinw3/leetcode-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
89 lines (84 loc) · 2.86 KB
/
Main.java
File metadata and controls
89 lines (84 loc) · 2.86 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main {
// public int[] findWidest(int n, int[] nums) {
// int[] result = new int[2];
// return result;
// }
public void paragraph(int m, int n, String[] lines) {
HashMap<String, Integer>[] map = new HashMap[n];
for (int i = 0; i < n; i++) {
map[i] = new HashMap<String, Integer>();
}
for (int i = 0; i < n; i++) {
String[] words = lines[i].split(" ");
for (int j = 0; j < words.length; j++) {
map[i].put(words[j], map[i].getOrDefault(words[j], 0) + 1);
}
}
for (int i = 0; i < m; i++) {
String[] words = lines[n + i - 1].split(" ");
int[] total = new int[n];
int max = 0;
int maxIndex = 0;
for (int k = 0; k < n; k++) {
for (int j = 0; j < words.length; j++) {
if (map[k].containsKey(words[j])) {
total[k] += map[k].get(words[j]);
}
}
}
for (int l = 0; l < n; l++) {
if (max < total[l]) {
max = total[l];
maxIndex = l;
}
}
System.out.println(lines[maxIndex]);
for (int k = 0; k < n; k++) {
total[k] = 0;
}
}
}
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//System.out.println(args.length);
Main test = new Main();
//test.findWidest(Integer.valueOf(args[0]), )
String line = "";
int count = 0;
int m = 0;
int n = 0;
String[] lines = new String[0];
try {
while ((line = br.readLine()) != null) {
if (count == 0) {
String[] nums = line.split(" ");
n = Integer.valueOf(nums[0]);
m = Integer.valueOf(nums[1]);
count++;
break;
}
lines[count - 1] = new String(line);
count++;
}
lines = new String[m + n];
while (count != 0 && (line = br.readLine()) != null) {
lines[count - 1] = new String(line);
count++;
if(count == m + n) {
break;
}
}
test.paragraph(m, n, lines);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}