The Indian Engineer

Problem 2825 Make String a Subsequence Using Cyclic Increments

Posted on 5 mins

String Two-Pointers

Problem Statement

Link - Problem 2825

Question

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:

Solution

class Solution {
public:
    bool canMakeSubsequence(string str1, string str2) {
        int i = 0;
        for(char ch:str1){
            if(i == str2.size())
                break;
            int a = ch -'a', b = str2[i]-'a';
            if(a==b || (a+1)%26 == b)
                i++;
        }
        return i == str2.size();

    }
};

Complexity Analysis

| Algorithm                    | Time Complexity | Space Complexity |
| ---------------------------- | --------------- | ---------------- |
| Two-pointer pattern matching | O(n)            | O(1)             |

Explanation

Intial Thoughts

When approaching this problem, consider the constraints given, such as the 0-indexed strings and the cyclic nature of the operation. Think of the relationship between the characters in str1 and str2. Observe how many characters in str2 can be formed from str1 using the operation. Consider how the length of str1 and str2 affect the outcome.

Intuitive Analysis

To intuitively solve this problem, imagine putting str1 and str2 side by side to compare their characters. Picture the characters in str1 shifting one position forward, wrapping around from ‘z’ to ‘a’, and see if this shift can make str2 a subsequence of str1. Consider how the characters in str2 match those in str1, with or without the shift, and think about how this match can be exploited to form str2 as a subsequence of str1.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Consider the indices we will increment separately.
We can maintain two pointers: pointer i for str1 and pointer j for str2, while ensuring they remain within the bounds of the strings.
If both str1[i] and str2[j] match, or if incrementing str1[i] matches str2[j], we increase both pointers; otherwise, we increment only pointer i.
It is possible to make str2 a subsequence of str1 if j is at the end of str2, after we can no longer find a match.

Similar Questions:

Title URL Difficulty
Is Subsequence https://leetcode.com/problems/is-subsequence Easy