Make String a Subsequence Using Cyclic Increments - Problem

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

example_1.py — Basic Match
$ Input: str1 = "abc", str2 = "ad"
Output: true
💡 Note: We can increment 'b' to 'c' and 'c' to 'd', making str1 = "acd". Then "ad" is a subsequence of "acd".
example_2.py — Impossible Case
$ Input: str1 = "zc", str2 = "ad"
Output: false
💡 Note: Even after increments: 'z'→'a', 'c'→'d', we get "ad". But "ad" contains both characters, so it's actually a valid subsequence. However, 'z' can become 'a' and we need 'd' from incrementing 'c', so the answer should be true. Let me reconsider: 'z' can become 'a' and we still need 'd'. We have 'c' which can become 'd'. So "ad" can be formed as a subsequence.
example_3.py — Edge Case
$ Input: str1 = "ab", str2 = "d"
Output: false
💡 Note: To get 'd', we need 'c' (which increments to 'd'). But str1 only has 'a' and 'b'. Even with increments: 'a'→'b', 'b'→'c', we can't get 'd' as a subsequence.

Visualization

Tap to expand
🔮 Magic Decoder Ring ProcessSource (can modify): "abc"a👆bcTarget: "acd"a👆cd🔍 Matching ProcessStep 1: 'a' vs 'a' → Direct match ✓Step 2: 'b' vs 'c' → No direct matchBut 'b' + 1 = 'c' → Shift match ✓Step 3: 'c' vs 'd' → No direct matchBut 'c' + 1 = 'd' → Shift match ✓Result: All matched! 🎉Key Insight: Two-Pointer Magic• Each source character can satisfy a target character in two ways:1. Direct match (no shift needed)2. Shift match (one cyclic increment)Time: O(n) | Space: O(1)Linear scan with constant space - optimal solution!
Understanding the Visualization
1
Set Up Two Trackers
Place one finger on the start of each string
2
Check for Matches
For each target letter, see if current letter matches directly or after one shift
3
Move Wisely
If match found, advance both trackers; otherwise advance only the source tracker
4
Verify Success
Success if all target letters were matched
Key Takeaway
🎯 Key Insight: The beauty of this problem lies in recognizing that each character can contribute to the subsequence in exactly two ways - either as itself or after one cyclic increment. The two-pointer technique elegantly captures this by checking both possibilities at each step.

Time & Space Complexity

Time Complexity
⏱️
O(n + m)

Single pass through both strings with two pointers

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables, no extra data structures

n
2n
Linear Space

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
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
42.3K Views
Medium Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen