You are given two strings s
and t
consisting of only lowercase English letters.
Return the minimum number of characters that need to be appended to the end of s
so that t
becomes a subsequence of s
.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Input: s = "coaching", t = "coding" Output: 4 Explanation: Append the characters "ding" to the end of s so that s = "coachingding". Now, t is a subsequence of s ("coachingding"). It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
Input: s = "abcde", t = "a" Output: 0 Explanation: t is already a subsequence of s ("abcde").
Input: s = "z", t = "abcde" Output: 5 Explanation: Append the characters "abcde" to the end of s so that s = "zabcde". Now, t is a subsequence of s ("zabcde"). It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
1 <= s.length, t.length <= 105
s
andt
consist only of lowercase English letters.
impl Solution {
pub fn append_characters(s: String, t: String) -> i32 {
let s = s.as_bytes();
let t = t.as_bytes();
let mut i = 0;
for j in 0..s.len() {
if i >= t.len() {
break;
} else if t[i] == s[j] {
i += 1;
}
}
(t.len() - i) as i32
}
}