Skip to content

Commit 1fd4b7b

Browse files
add: replacing-with-the-cipher-letters
1 parent e502a84 commit 1fd4b7b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- [Filling all the missed angle brackets](filling-all-the-missed-angle-brackets)
44
- [Finding all the anagrams](finding-all-the-anagrams)
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
6+
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Replacing with the cipher letters
2+
3+
- The plain and cipher letters should be lowercase(a-z).
4+
- The plain letter should be replaced with the cipher letter of same position.
5+
6+
e.g.
7+
8+
Input
9+
10+
```console
11+
helloworld
12+
```
13+
14+
Output
15+
16+
```console
17+
svooldliow
18+
```
19+
20+
## Execute
21+
22+
```bash
23+
node solution.js
24+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const solution = (word, cipher) => {
2+
if (
3+
!/^[a-z]+$/.test(word) ||
4+
!word ||
5+
!cipher ||
6+
cipher.length !== 26
7+
) {
8+
return '';
9+
}
10+
let result = '';
11+
const aCode = 'a'.charCodeAt(0);
12+
for (let i = 0; i < word.length; i++) {
13+
const index = word.charCodeAt(i) - aCode;
14+
if (index < 0 || index >= cipher.length) {
15+
continue;
16+
}
17+
result += cipher.charAt(index);
18+
}
19+
return result;
20+
};
21+
22+
(() => {
23+
console.log(
24+
solution('helloworld', 'zyxwvutsrqponmlkjihgfedcba'),
25+
);
26+
})();

0 commit comments

Comments
 (0)