-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollidingEncoding.java
More file actions
67 lines (58 loc) · 1.83 KB
/
CollidingEncoding.java
File metadata and controls
67 lines (58 loc) · 1.83 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
/***********************************
* AlgoSolutions Project
* Filename: CollidingEncoding.java
* Author : malay
* Date : 15-Apr-2023
*
**********************************/
package com.contest.codejam.y2023;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class CollidingEncoding {
public static void main(String[] args) {
Scanner s = new Scanner((Readable) new BufferedReader(new InputStreamReader(System.in)));
int noOfTestCases = s.nextInt();
s.nextLine();
for (int i = 0; i < noOfTestCases; i++) {
String line = s.nextLine();
Map<Integer, Integer> encodingToUsed = convertEncodingByAscii(line);
long noOfWords = s.nextLong();
s.nextLine();
Set<String> encodedSet = new HashSet<String>();
boolean isCollisionPresent = false;
for (int j = 0; j < noOfWords; j++) {
String str = s.nextLine();
boolean result = encodedSet.add(encodeUsingMap(str, encodingToUsed));
if (!result) {
isCollisionPresent = true;
}
}
if (isCollisionPresent) {
System.out.println("Case #" + (i + 1) + ": YES");
} else {
System.out.println("Case #" + (i + 1) + ": NO");
}
}
s.close();
}
private static String encodeUsingMap(String str, Map<Integer, Integer> encodingToUsed) {
StringBuilder result = new StringBuilder();
for (char character : str.toCharArray()) {
result.append(encodingToUsed.get((int) character));
}
return result.toString();
}
private static Map<Integer, Integer> convertEncodingByAscii(String line) {
Map<Integer, Integer> encodingHolder = new HashMap<Integer, Integer>(26);
int asciiCode = 65;
for (String enc : line.split(" ")) {
encodingHolder.put(asciiCode++, Integer.parseInt(enc));
}
return encodingHolder;
}
}