2486. Append Characters to String to Make Subsequence LeetCode Solution
In this guide, you will get 2486. Append Characters to String to Make Subsequence LeetCode Solution with the best time and space complexity. The solution to Append Characters to String to Make Subsequence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Append Characters to String to Make Subsequence solution in C++
- Append Characters to String to Make Subsequence solution in Java
- Append Characters to String to Make Subsequence solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
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.
Example 2:
Input: s = “abcde”, t = “a”
Output: 0
Explanation: t is already a subsequence of s (“abcde”).
Example 3:
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.
Constraints:
1 <= s.length, t.length <= 105
s and t consist only of lowercase English letters.
Complexity Analysis
- Time Complexity: O(|\texttt{s}|)
- Space Complexity: O(1)
2486. Append Characters to String to Make Subsequence LeetCode Solution in C++
class Solution {
public:
int appendCharacters(string s, string t) {
int i = 0; // t's index
for (const char c : s)
if (c == t[i])
if (++i == t.length())
return 0;
return t.length() - i;
}
};
/* code provided by PROGIEZ */
2486. Append Characters to String to Make Subsequence LeetCode Solution in Java
class Solution {
public int appendCharacters(String s, String t) {
int i = 0; // t's index
for (final char c : s.toCharArray())
if (c == t.charAt(i))
if (++i == t.length())
return 0;
return t.length() - i;
}
}
// code provided by PROGIEZ
2486. Append Characters to String to Make Subsequence LeetCode Solution in Python
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
i = 0 # t's index
for c in s:
if c == t[i]:
i += 1
if i == len(t):
return 0
return len(t) - i
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.