-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCitiesAndStates.java
More file actions
37 lines (37 loc) · 1.17 KB
/
CitiesAndStates.java
File metadata and controls
37 lines (37 loc) · 1.17 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
import java.io.*;
import java.util.*;
public class CitiesAndStates {
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("citystate.in"));
HashMap<String, Integer> names = new HashMap<String, Integer>();
int num = Integer.parseInt(br.readLine());
String[] temp;
String key;
for (int i = 0; i < num; i++)
{
temp = br.readLine().split(" ");
key = temp[0].substring(0, 2) + temp[1]; //smart key value for hashmap
if (names.containsKey(key))
names.put(key, names.get(key) + 1);
else
names.put(key, 1);
}
System.out.println(names);
ArrayList<String> keys = new ArrayList<String>(names.keySet());
int total = 0;
for (String k: keys)
{
if (names.containsKey(k.substring(2)+k.substring(0, 2)) && (k.substring(2)+k.substring(0, 2)).compareTo(k)!= 0)
{
total += names.get(k) * names.get(k.substring(2) + k.substring(0, 2));
}
}
//System.out.println(total/2);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("citystate.out")));
pw.println(total/2);
pw.close();
br.close();
}
}