Imagine you have a special typewriter that can perform a cyclic character shift - where you can transform any letter to the next one in the alphabet (with 'z' wrapping around to 'a'). Your goal is to determine if you can make one string a subsequence of another using this magical transformation.
You're given two strings: str1 (the string you can modify) and str2 (the target subsequence). In a single operation, you can select any set of characters in str1 and increment each one cyclically:
- 'a' → 'b', 'b' → 'c', ..., 'z' → 'a'
Return true if you can make str2 a subsequence of str1 by performing this operation at most once, otherwise return false.
Remember: A subsequence maintains the relative order of characters but allows deletions from the original string.
Input & Output
Visualization
Time & Space Complexity
Single pass through both strings with two pointers
Only using a few variables, no extra data structures
Constraints
- 1 ≤ str1.length, str2.length ≤ 105
- str1 and str2 consist only of lowercase English letters
- You can perform the cyclic increment operation at most once