-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJava.java
71 lines (59 loc) · 2.21 KB
/
Java.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
67
68
69
70
71
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: MIME Type */
/* Difficulty: Easy */
/* Date solved: 08.11.2018 */
/* */
/****************************************/
import java.util.HashMap;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read inputs.
Scanner scanner = new Scanner(System.in);
int N = Integer.parseInt(scanner.nextLine());
int Q = Integer.parseInt(scanner.nextLine());
//Creating hashmap: extension -> mimetype
HashMap<String, String> mimeTypes = new HashMap<String, String>();
for (int i = 0; i < N; i++)
{
String[] inputs = scanner.nextLine().split(" ");
String EXTENSION = inputs[0];
String MIMETYPE = inputs[1];
//Fill the hashmap.
mimeTypes.put(EXTENSION.toLowerCase(), MIMETYPE);
}
for (int i = 0; i < Q; i++)
{
String FILENAME = scanner.nextLine();
String FILEEXT = GetFileExtension(FILENAME).toLowerCase();
//Check if the file's extension is known.
if (mimeTypes.containsKey(FILEEXT))
{
//Output the corresponding MIME type if file's extension is known.
System.out.println(mimeTypes.get(FILEEXT));
}
else
{
//Output "UNKNKOWN" if file's extension is not known.
System.out.println("UNKNOWN");
}
}
}
//Gets a file's extension without the dot itself. If the file has no extension it will return "".
private static String GetFileExtension(String file)
{
//Find the beginning of an extension.
int extensionIndex = file.lastIndexOf('.');
if (extensionIndex < 0)
{
//There is no extension.
return "";
}
return file.substring(extensionIndex + 1);
}
}