Skip to content

Commit ff0c7a2

Browse files
committed
feat(leetcode): 2825 Make String a Subsequence Using Cyclic Increments
1 parent afbf3fa commit ff0c7a2

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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>&#39;a&#39;</code> becomes <code>&#39;b&#39;</code>, <code>&#39;b&#39;</code> becomes <code>&#39;c&#39;</code>, and so on, and <code>&#39;z&#39;</code> becomes <code>&#39;a&#39;</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>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> str1 = &quot;abc&quot;, str2 = &quot;ad&quot;
15+
<strong>Output:</strong> true
16+
<strong>Explanation:</strong> Select index 2 in str1.
17+
Increment str1[2] to become &#39;d&#39;.
18+
Hence, str1 becomes &quot;abd&quot; 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 = &quot;zc&quot;, str2 = &quot;ad&quot;
24+
<strong>Output:</strong> true
25+
<strong>Explanation:</strong> Select indices 0 and 1 in str1.
26+
Increment str1[0] to become &#39;a&#39;.
27+
Increment str1[1] to become &#39;d&#39;.
28+
Hence, str1 becomes &quot;ad&quot; 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 = &quot;ab&quot;, str2 = &quot;d&quot;
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>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= str1.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= str2.length &lt;= 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>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/
4+
* Runtime: 0ms (100.00%)
5+
*/
6+
7+
const static auto _ = []() {
8+
cin.tie(nullptr)->sync_with_stdio(false);
9+
return nullptr;
10+
}();
11+
12+
class Solution {
13+
public:
14+
static bool canMakeSubsequence(string &str1, string &str2) {
15+
const int sourceLength = str1.size(), targetLength = str2.size();
16+
char currentTargetChar = str2[0];
17+
18+
int sourceIndex, targetIndex;
19+
for (sourceIndex = 0, targetIndex = 0;
20+
sourceIndex < sourceLength &&
21+
targetIndex < targetLength;
22+
sourceIndex++) {
23+
char currentSourceChar = str1[sourceIndex];
24+
25+
if (currentSourceChar == currentTargetChar ||
26+
currentSourceChar + 1 == currentTargetChar ||
27+
(currentSourceChar == 'z' && currentTargetChar == 'a')) {
28+
currentTargetChar = str2[++targetIndex];
29+
}
30+
}
31+
32+
return targetIndex == targetLength;
33+
}
34+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/
4+
* Runtime: ms (%)
5+
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
* Author: 1chooo<hugo970217@gmail.com>
3+
* Problem: https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments/
4+
* Runtime: ms (%)
5+
"""
6+

0 commit comments

Comments
 (0)