#2486
Medium Algorithms Append characters to string to make subsequence
Two Pointers String Greedy
73.0% acceptance
Feb 25, 2026
1187
90
You are given two strings s and t consisting only of lowercase English letters.
Return the minimum number of characters to append to s so that t becomes a subsequence of s.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn append_characters(s: String, t: String) -> i32 {
let s = s.as_bytes();
let t = t.as_bytes();
let mut j = 0usize;
for &c in s {
if j < t.len() && c == t[j] { j += 1; }
}
(t.len() - j) as i32
}
}