[LeetCode] Append Characters to String to Make Subsequence

2486. Append Characters to String to Make Subsequence

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int appendCharacters(string s, string t) {
int i = 0, j = 0;
for(; i < s.length() and j < t.length(); i++) {
if(s[i] == t[j]) j++;
}
return t.length() - j;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/27/PS/LeetCode/append-characters-to-string-to-make-subsequence/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.