File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed
replacing-with-the-cipher-letters Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments