-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path059-xor-decryption.js
More file actions
66 lines (53 loc) · 1.59 KB
/
059-xor-decryption.js
File metadata and controls
66 lines (53 loc) · 1.59 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
/**
* XOR Decryption
* Time Complexity: O(26^3 * N)
* Space Complexity: O(N)
*/
function processData(input) {
const parts = input.trim().split(/\s+/).map(Number);
const N = parts[0];
const arr = parts.slice(1);
function isValidChar(code) {
if (code === 32) return true;
if (code >= 48 && code <= 57) return true;
if (code >= 65 && code <= 90) return true;
if (code >= 97 && code <= 122) return true;
const allowed = new Set([
33, 34, 39, 40, 41, 44, 45, 46, 58, 59, 63
]);
return allowed.has(code);
}
for (let a = 97; a <= 122; a++) {
for (let b = 97; b <= 122; b++) {
for (let c = 97; c <= 122; c++) {
const key = [a, b, c];
let ok = true;
for (let i = 0; i < N; i++) {
const keyByte = key[i % 3];
const decoded = arr[i] ^ keyByte;
if (!isValidChar(decoded)) {
ok = false;
break;
}
}
if (ok) {
console.log(
String.fromCharCode(a) +
String.fromCharCode(b) +
String.fromCharCode(c)
);
return;
}
}
}
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});