|
| 1 | +## [2825. Make String a Subsequence Using Cyclic Increments](https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/) |
| 2 | +<p>You are given two <strong>0-indexed</strong> strings <code>str1</code> and <code>str2</code>.</p> |
| 3 | + |
| 4 | +<p>In an operation, you select a <strong>set</strong> of indices in <code>str1</code>, and for each index <code>i</code> in the set, increment <code>str1[i]</code> to the next character <strong>cyclically</strong>. That is <code>'a'</code> becomes <code>'b'</code>, <code>'b'</code> becomes <code>'c'</code>, and so on, and <code>'z'</code> becomes <code>'a'</code>.</p> |
| 5 | + |
| 6 | +<p>Return <code>true</code> <em>if it is possible to make </em><code>str2</code> <em>a subsequence of </em><code>str1</code> <em>by performing the operation <strong>at most once</strong></em>, <em>and</em> <code>false</code> <em>otherwise</em>.</p> |
| 7 | + |
| 8 | +<p><strong>Note:</strong> 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.</p> |
| 9 | + |
| 10 | +<p> </p> |
| 11 | +<p><strong class="example">Example 1:</strong></p> |
| 12 | + |
| 13 | +<pre> |
| 14 | +<strong>Input:</strong> str1 = "abc", str2 = "ad" |
| 15 | +<strong>Output:</strong> true |
| 16 | +<strong>Explanation:</strong> Select index 2 in str1. |
| 17 | +Increment str1[2] to become 'd'. |
| 18 | +Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.</pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> str1 = "zc", str2 = "ad" |
| 24 | +<strong>Output:</strong> true |
| 25 | +<strong>Explanation:</strong> Select indices 0 and 1 in str1. |
| 26 | +Increment str1[0] to become 'a'. |
| 27 | +Increment str1[1] to become 'd'. |
| 28 | +Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.</pre> |
| 29 | + |
| 30 | +<p><strong class="example">Example 3:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> str1 = "ab", str2 = "d" |
| 34 | +<strong>Output:</strong> false |
| 35 | +<strong>Explanation:</strong> In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. |
| 36 | +Therefore, false is returned.</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= str1.length <= 10<sup>5</sup></code></li> |
| 43 | + <li><code>1 <= str2.length <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>str1</code> and <code>str2</code> consist of only lowercase English letters.</li> |
| 45 | +</ul> |
| 46 | + |
| 47 | + |
| 48 | +## Hints |
| 49 | +1. <div class="_1l1MA">Consider the indices we will increment separately.</div> |
| 50 | +2. <div class="_1l1MA">We can maintain two pointers: pointer <code>i</code> for <code>str1</code> and pointer <code>j</code> for <code>str2</code>, while ensuring they remain within the bounds of the strings.</div> |
| 51 | +3. <div class="_1l1MA">If both <code>str1[i]</code> and <code>str2[j]</code> match, or if incrementing <code>str1[i]</code> matches <code>str2[j]</code>, we increase both pointers; otherwise, we increment only pointer <code>i</code>.</div> |
| 52 | +4. <div class="_1l1MA">It is possible to make <code>str2</code> a subsequence of <code>str1</code> if <code>j</code> is at the end of <code>str2</code>, after we can no longer find a match.</div> |
0 commit comments