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 <= 105s and t consist only of lowercase English letters.This approach uses two pointers to scan both strings, one pointer for each string. The idea is to attempt to match characters of t with those in s. A character from t is successfully matched if it is equal to the current character in s. We iterate through s, moving the pointer for each character matched, while the pointer for t only moves if a match is found. Once we've iterated through s, we determine how many characters are left unmatched in t, which is the number of characters needed to be appended to s.
The C solution declares a function minCharsToAppend that takes two strings, s and t, and calculates the minimum number of characters required to append to s to make t a subsequence. We use two indices to traverse the strings, incrementing sIndex for each iteration and only incrementing tIndex when characters match. Finally, we calculate the number of unmatched characters in t and return it.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n + m) where n is the length of s and m is the length of t.
Space Complexity: O(1) as no additional space is used.
This approach uses dynamic programming to precompute the next occurrence of each character for every position in the string s. By doing so, we quickly identify the existence and location of each character from t in s. We build a 2D array next where next[i][j] represents the smallest index >= i that holds the character j in s. This helps in efficiently determining the position of characters and the necessary additions to the end of the string.
buildNext function precomputes the next occurrences of each character for every index. Using this information, we find out how to move through s to match t and calculate how many characters in t are not matched, hence needing to be appended.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n + m + n * ALPHABET_SIZE) due to preprocessing each character for positions.
Space Complexity: O(n * ALPHABET_SIZE) used by the 2D array next where n is length of s.
| Approach | Complexity |
|---|---|
| Two-Pointer Approach | Time Complexity: O(n + m) where n is the length of |
| Dynamic Programming Approach | Time Complexity: O(n + m + n * ALPHABET_SIZE) due to preprocessing each character for positions. |
Append Characters to Strings to Make Subsequence - Leetcode 2486 - Python • NeetCodeIO • 6,465 views views
Watch 9 more video solutions →Practice Append Characters to String to Make Subsequence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor