Leetcode 2825: Make String a Subsequence Using Cyclic Increments

grid47
grid47
Exploring patterns and algorithms
Jan 29, 2024 4 min read

Given two strings, str1 and str2, you can perform an operation on str1 where you select a set of indices and increment the characters at those indices cyclically. The task is to check if it is possible to make str2 a subsequence of str1 by performing the operation at most once.
Problem
Approach
Steps
Complexity
Input: Two strings `str1` and `str2` consisting only of lowercase English letters.
Example: str1 = 'abc', str2 = 'ad'
Constraints:
• 1 <= str1.length <= 10^5
• 1 <= str2.length <= 10^5
• str1 and str2 consist of only lowercase English letters.
Output: Return `true` if it is possible to make `str2` a subsequence of `str1` by performing the operation at most once, otherwise return `false`.
Example: For `str1 = 'abc'`, `str2 = 'ad'`, the output should be `true`.
Constraints:
Goal: The task is to check if `str2` can be a subsequence of `str1` after performing the operation once.
Steps:
• Loop through the characters of `str1` and try to match them with `str2`.
• If a match is found, check if the character in `str1` can be incremented to match the character in `str2`.
Goal: Constraints on the lengths and content of `str1` and `str2`.
Steps:
• Both `str1` and `str2` are non-empty.
• The strings consist only of lowercase English letters.
Assumptions:
• The strings consist only of lowercase English letters.
• The operation can be applied at most once.
Input: For `str1 = 'abc'`, `str2 = 'ad'`
Explanation: By incrementing the character at index 2 in `str1` to 'd', `str1` becomes 'abd' and `str2` is now a subsequence of `str1`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus