-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1844-replace-all-digits-with-characters.js
More file actions
39 lines (35 loc) · 1.14 KB
/
1844-replace-all-digits-with-characters.js
File metadata and controls
39 lines (35 loc) · 1.14 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
/**
* 1844. Replace All Digits with Characters
* https://leetcode.com/problems/replace-all-digits-with-characters/
* Difficulty: Easy
*
* You are given a 0-indexed string s that has lowercase English letters in its even indices and
* digits in its odd indices.
*
* You must perform an operation shift(c, x), where c is a character and x is a digit, that returns
* the xth character after c.
* - For example, shift('a', 5) = 'f' and shift('x', 0) = 'x'.
*
* For every odd index i, you want to replace the digit s[i] with the result of the
* shift(s[i-1], s[i]) operation.
*
* Return s after replacing all digits. It is guaranteed that shift(s[i-1], s[i]) will never
* exceed 'z'.
*
* Note that shift(c, x) is not a preloaded function, but an operation to be implemented as part
* of the solution.
*/
/**
* @param {string} s
* @return {string}
*/
var replaceDigits = function(s) {
const result = s.split('');
for (let i = 1; i < s.length; i += 2) {
result[i] = shiftChar(s[i - 1], s[i]);
}
return result.join('');
function shiftChar(char, shift) {
return String.fromCharCode(char.charCodeAt(0) + parseInt(shift));
}
};