Skip to main content
Back to problems
#392
Easy Algorithms

Is subsequence

Two Pointers String Dynamic Programming
48.9% acceptance
Jan 12, 2026
10771
619
Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_subsequence(s: String, t: String) -> bool {
    let s_bytes: Vec<u8> = s.bytes().collect();
    let t_bytes: Vec<u8> = t.bytes().collect();
    
    if s_bytes.is_empty() {
      return true;
    }
    
    let mut s_idx = 0;
    
    for &b in &t_bytes {
      if b == s_bytes[s_idx] {
        s_idx += 1;
        if s_idx == s_bytes.len() {
          return true;
        }
      }
    }
    
    false
  }
}