8000 Create 01-Message Decryption.js · aachal28/learn-javascript@120dec4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 120dec4

Browse files
authored
Create 01-Message Decryption.js
1 parent b957a54 commit 120dec4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Que : Dave needs to decode a message from his friend Bruce. The message contains an encoded string, and Dave needs to find the longest "good" substring within it. A "good" substring follows these rules:
2+
3+
Each character is identical to the previous one.
4+
Each character is either the preceding or following character in the English alphabet.
5+
Given the encoded message, your task is to help Dave find the length of the longest "good" substring.
6+
7+
For example:
8+
9+
Input: "abcef"
10+
Output: 2
11+
12+
function messageDecrypt(W) {
13+
let maxLength = 0;
14+
let currentLength = 1;
15+
16+
for (let i = 1; i < W.length; i++) {
17+
if (W[i] === W[i - 1] || Math.abs(W.charCodeAt(i) - W.charCodeAt(i - 1)) === 1 || Math.abs(W.charCodeAt(i) - W.charCodeAt(i - 1)) === 25) {
18+
currentLength++;
19+
} else {
20+
maxLength = Math.max(maxLength, currentLength);
21+
currentLength = 1;
22+
}
23+
}
24+
25+
maxLength = Math.max(maxLength, currentLength);
26+
return maxLength;
27+
}
28+
29+
// Example usage:
30+
const W = "abcef";
31+
console.log(messageDecrypt(W)); // Output: 2

0 commit comments

Comments
 (0)
0