I have a quick question on taking an encrypted text file's content and swapping out the corresponding letters based on a given frequency list.
For instance, the first few lines in the text file are:
XWJGFBVCCVUJYPXQZGCVXQHXKAZGPVPUHBZJYPUVMFCZVYXQZWJFZJCCMVMGHWZBZXQZFKBKUBKAZGJY
I created an ArrayList of the individual characters and then found how many times each letter appears in the file in the HashSet.
A: 6
B: 35
C: 30
D: 21
F: 31
G: 38
H: 16
I: 1
J: 61
K: 53
L: 16
M: 26
N: 7
O: 3
P: 33
Q: 61
R: 10
S: 8
U: 21
V: 37
W: 23
X: 67
Y: 36
Z: 80
import java.io.*;
import java.util.*;
public class lab9 {
lab9() {
try {
String content = new Scanner(new File("Cipher.txt")).useDelimiter("\\Z").next();
String letters = content.replaceAll("\\W", "");
System.out.println(letters);
List < String > list = new ArrayList < String > (Arrays.asList(letters.split("")));
Set < String > unique = new HashSet < String > (list);
for (String key: unique) {
System.out.println(key + ": " + Collections.frequency(list, key));
}
String decipher = "ETAHOSINRDBLMWGUCYFJKVPXQZ";
List < String > list2 = new ArrayList < String > (Arrays.asList(decipher.split("")));
System.out.println(decipher);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the code snippet, you can see the String decipher which is a list of the most commonly used letters in the English language in descending order. Thus, I want to swap the letter Z with E in the text file. What would be an efficient way of doing this?