-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20230116.js
84 lines (58 loc) · 1.59 KB
/
20230116.js
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
Lang: JavaScript
Type: fastest
Problem:
Encrypt a message, m, given an encryption key, k. The original message was encrypted using Domino encryption, which goes through each letter k_i in k, and replaces all occurrences of k_i in m with k_i+1. The last letter of k is replaced by the first letter of k.
k has no duplicate letters.
Not every letter of m is necessarily in k.
Not every letter of k is necessarily in m.
Uppercase and lowercase letters are not the same.
Example:
m = Hello World
k = HeloWrd
Using k, the encryption would use the following conversion:
H -> e
e -> l
o -> W
W -> r
r -> d
d -> H
The answer should be: elooW rWdoH ( because using k, m = Hello World would be encrypted to elooW rWdoH )
Input
Line 1: Plain text message, m
Line 2: Encryption key, k
Output
Line 1: Encrypted message
Constraints
m is no longer than 1000 characters
k only contains uppercase and lowercase alphabetic characters
k does not contain any duplicates (and is therefore no longer than 52 characters i.e. 26 uppercase + 26 lowercase characters)
Example
Input
Hello World
HeloWrd
Output
elooW rWdoH
Solution:
const m = readline();
const k = readline();
let encrypt = k.split('')
let dicionary = {}
encrypt.forEach( (e, i) => {
if (i+1 == encrypt.length) {
dicionary[e] = encrypt[0]
} else {
dicionary[e] = encrypt[i+1]
}
})
let original = m.split('')
let newword = ''
original.forEach( (e) => {
if(e == ' ') {
newword += ' '
} else if (e in dicionary) {
newword += dicionary[e]
} else {
newword += e
}
})
console.log(newword);