Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
131 lines (104 loc) · 3.88 KB

File metadata and controls

131 lines (104 loc) · 3.88 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Solution {
static final String DICTIONARY_FILE_PATH = "dictionary.lst";
public static void main(String[] args) throws Throwable {
List<String> dictionary = readDictionary();
List<String> cipherWords = readCiphertext();
Map<Character, Character> decodeMap = new HashMap<Character, Character>();
decode(dictionary, cipherWords, decodeMap);
System.out.println(String.join(" ", cipherWords.stream().map(cipherWord -> decodeWord(decodeMap, cipherWord))
.collect(Collectors.toList())));
}
static boolean decode(List<String> dictionary, List<String> cipherWords, Map<Character, Character> decodeMap) {
boolean allDone = true;
String chosenCipherWord = null;
List<String> chosenMatchedDictionary = null;
for (String cipherWord : cipherWords) {
if (!isAllMapped(cipherWord, decodeMap)) {
allDone = false;
List<String> matchedDictionary = match(cipherWord, dictionary, decodeMap);
if (chosenMatchedDictionary == null || matchedDictionary.size() < chosenMatchedDictionary.size()) {
chosenCipherWord = cipherWord;
chosenMatchedDictionary = matchedDictionary;
}
}
}
if (allDone) {
return true;
}
if (chosenMatchedDictionary.isEmpty()) {
return false;
}
for (String chosenMatchedDictionaryWord : chosenMatchedDictionary) {
Map<Character, Character> addedDecodeMap = buildAddedDecodeMap(chosenCipherWord,
chosenMatchedDictionaryWord, decodeMap);
decodeMap.putAll(addedDecodeMap);
if (decode(dictionary, cipherWords, decodeMap)) {
return true;
}
for (char addedKey : addedDecodeMap.keySet()) {
decodeMap.remove(addedKey);
}
}
return false;
}
static List<String> match(String cipherWord, List<String> dictionary, Map<Character, Character> decodeMap) {
return dictionary.stream().filter(dictionaryWord -> isMapped(cipherWord, dictionaryWord, decodeMap))
.collect(Collectors.toList());
}
static boolean isMapped(String cipherWord, String dictionaryWord, Map<Character, Character> decodeMap) {
if (cipherWord.length() != dictionaryWord.length()) {
return false;
}
for (int i = 0; i < cipherWord.length(); i++) {
char cipherLetter = cipherWord.charAt(i);
if (decodeMap.containsKey(cipherLetter) && decodeMap.get(cipherLetter) != dictionaryWord.charAt(i)) {
return false;
}
}
return true;
}
static Map<Character, Character> buildAddedDecodeMap(String cipherWord, String dictionaryWord,
Map<Character, Character> decodeMap) {
Map<Character, Character> addedDecodeMap = new HashMap<Character, Character>();
for (int i = 0; i < cipherWord.length(); i++) {
char cipherLetter = cipherWord.charAt(i);
if (decodeMap.containsKey(cipherLetter)) {
continue;
}
addedDecodeMap.put(cipherLetter, dictionaryWord.charAt(i));
}
return addedDecodeMap;
}
static boolean isAllMapped(String cipherWord, Map<Character, Character> decodeMap) {
return cipherWord.chars().allMatch(letter -> decodeMap.containsKey((char) letter));
}
static String decodeWord(Map<Character, Character> decodeMap, String cipherWord) {
return cipherWord.chars().mapToObj(cipherLetter -> decodeMap.get((char) cipherLetter))
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
}
static List<String> readCiphertext() {
List<String> cipherWords = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
cipherWords.add(sc.next());
}
sc.close();
return cipherWords;
}
static List<String> readDictionary() throws Throwable {
List<String> dictionary = new ArrayList<String>();
Scanner sc = new Scanner(new File(DICTIONARY_FILE_PATH));
while (sc.hasNext()) {
dictionary.add(sc.next().toLowerCase());
}
sc.close();
return dictionary;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.