2825. Make String a Subsequence Using Cyclic Increments
Difficulty: Medium
Topics: Two Pointers, String
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:
- 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.
Constraints:
1 <= str1.length <= 1051 <= str2.length <= 105-
str1andstr2consist of only lowercase English letters.
Hint:
- Consider the indices we will increment separately.
- We can maintain two pointers: pointer
iforstr1and pointerjforstr2, while ensuring they remain within the bounds of the strings. - If both
str1[i]andstr2[j]match, or if incrementingstr1[i]matchesstr2[j], we increase both pointers; otherwise, we increment only pointeri. - It is possible to make
str2a subsequence ofstr1ifjis at the end ofstr2, after we can no longer find a match.
Solution:
We need to check if we can make str2 a subsequence of str1 by performing at most one cyclic increment operation on any characters in str1:
Explanation:
- We will use two pointers,
iforstr1andjforstr2. - If the character at
str1[i]matchesstr2[j], we move both pointers forward. - If
str1[i]can be incremented to matchstr2[j](cyclically), we try to match them and then move both pointers. - If neither of the above conditions holds, we only move the pointer
iforstr1. - Finally, if we can match all characters of
str2, then it is possible to makestr2a subsequence ofstr1, otherwise not.
Let's implement this solution in PHP: 2825. Make String a Subsequence Using Cyclic Increments
<?php
/**
* @param String $str1
* @param String $str2
* @return Boolean
*/
function canMakeSubsequence($str1, $str2) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example Usage
$str1 = "abc";
$str2 = "ad";
echo canMakeSubsequence($str1, $str2) ? 'true' : 'false'; // Output: true
$str1 = "zc";
$str2 = "ad";
echo canMakeSubsequence($str1, $str2) ? 'true' : 'false'; // Output: true
$str1 = "ab";
$str2 = "d";
echo canMakeSubsequence($str1, $str2) ? 'true' : 'false'; // Output: false
?>
Explanation:
-
Two Pointers:
iandjare initialized to the start ofstr1andstr2, respectively. -
Matching Logic: Inside the loop, we check if the characters at
str1[i]andstr2[j]are the same or if we can incrementstr1[i]to matchstr2[j]cyclically.- The cyclic increment condition is handled using
(ord($str1[$i]) + 1 - ord('a')) % 26which checks ifstr1[i]can be incremented to matchstr2[j].
- The cyclic increment condition is handled using
-
Subsequence Check: If we have iterated through
str2completely (i.e.,j == m), it meansstr2is a subsequence ofstr1. Otherwise, it's not.
Time Complexity:
- The algorithm iterates through
str1once, and each character instr2is checked only once, so the time complexity is O(n), wherenis the length ofstr1.
Space Complexity:
- The space complexity is O(1) since we only use a few pointers and do not need extra space dependent on the input size.
This solution efficiently checks if it's possible to make str2 a subsequence of str1 with at most one cyclic increment operation.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)