-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2825-make-string-a-subsequence-using-cyclic-increments.js
More file actions
39 lines (36 loc) · 1.21 KB
/
2825-make-string-a-subsequence-using-cyclic-increments.js
File metadata and controls
39 lines (36 loc) · 1.21 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
/**
* 2825. Make String a Subsequence Using Cyclic Increments
* https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/
* Difficulty: Medium
*
* You are given two 0-indexed strings str1 and str2.
*
* In an operation, you select a set of indices in str1, and for each index i in the set,
* increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes
* 'c', and so on, and 'z' becomes 'a'.
*
* Return true if it is possible to make str2 a subsequence of str1 by performing the operation
* at most once, and false otherwise.
*
* Note: A subsequence of a string is a new string that is formed from the original string by
* deleting some (possibly none) of the characters without disturbing the relative positions
* of the remaining characters.
*/
/**
* @param {string} str1
* @param {string} str2
* @return {boolean}
*/
var canMakeSubsequence = function(str1, str2) {
let i = 0;
let j = 0;
while (i < str1.length && j < str2.length) {
const curr = str1[i].charCodeAt(0);
const next = curr === 122 ? 97 : curr + 1;
if (str1[i] === str2[j] || String.fromCharCode(next) === str2[j]) {
j++;
}
i++;
}
return j === str2.length;
};