-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2825. Make String a Subsequence Using Cyclic Increments
- Loading branch information
1 parent
955df7a
commit dc36b57
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution | ||
{ | ||
public: | ||
bool canMakeSubsequence(string s, string t) | ||
{ | ||
// Step 1: Initialize two pointers | ||
int j = 0; // Pointer for string t | ||
|
||
// Step 2: Loop through string s | ||
for (int i = 0; i < s.size() && j < t.size(); i++) | ||
{ | ||
// Step 3: Get the current character in s | ||
char current = s[i]; | ||
|
||
// Step 4: Compute the cyclic increment | ||
char next = (current == 'z') ? 'a' : (current + 1); | ||
|
||
// Step 5: Check if current or its cyclic increment matches t[j] | ||
if (current == t[j] || next == t[j]) | ||
{ | ||
j++; // Move to the next character in t | ||
} | ||
} | ||
|
||
// Step 6: If we have matched all characters of t, return true | ||
return j == t.size(); | ||
} | ||
}; |