Skip to main content
Back to problems
#2825
Medium Algorithms

Make string a subsequence using cyclic increments

Two Pointers String
65.7% acceptance
Feb 25, 2026
878
72
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn can_make_subsequence(str1: String, str2: String) -> bool {
    let s1 = str1.as_bytes();
    let s2 = str2.as_bytes();
    let mut j = 0;
    for &c in s1 {
      if j < s2.len() && (c == s2[j] || (c - b'a' + 1) % 26 + b'a' == s2[j]) {
        j += 1;
      }
    }
    j == s2.len()
  }
}