Make String a Subsequence Using Cyclic Increments

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.

Example 1
Inputstr1 = "abc", str2 = "ad"
Outputtrue
Select index 2 in str1, increment str1[2] to become 'd', and then str1 becomes "abd", so str2 is a subsequence.
Example 2
Inputstr1 = "zc", str2 = "ad"
Outputtrue
Select indices 0 and 1 in str1, increment them to make str1 become "ad", so str2 is a subsequence.

Constraints

  • 1 <= str1.length <= 10^5
  • 1 <= str2.length <= 10^5
  • str1 and str2 consist of only lowercase English letters.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate