#2825

Make String a Subsequence Using Cyclic Increments

specialist · 630 · lc medium +30 · verified · 65.7% accepted · 879 likes · top 71%

Description

Two 0-indexed strings str1 and str2 are given.

In one operation, pick any subset of indices of str1 and cyclically increment each chosen character (so 'z' wraps to 'a').

Return true if str2 can be made a subsequence of str1 after performing the operation at most once, otherwise false.

Note: A subsequence is formed from the original string by removing some (possibly no) characters while keeping the remaining ones in order.

Example 1:

Input: str1 = "abc", str2 = "ad"
Output: true
Explanation: Select index 2 in str1.
Increment str1[2] to become 'd'.
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.

Example 2:

Input: str1 = "zc", str2 = "ad"
Output: true
Explanation: Select indices 0 and 1 in str1.
Increment str1[0] to become 'a'.
Increment str1[1] to become 'd'.
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.

Example 3:

Input: str1 = "ab", str2 = "d"
Output: false
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once.
Therefore, false is returned.

Code

1
2
3