Sponsored
Sponsored
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
.
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.
1using System;
2
3public class MinChars
4{
5 public static int MinCharsToAppend(string s, string t)
6 {
7 int sLen = s.Length;
8 int tLen = t.Length;
9 int sIndex = 0, tIndex = 0;
10
11 while (sIndex < sLen && tIndex < tLen)
12 {
13 if (s[sIndex] == t[tIndex])
14 {
tIndex++;
}
sIndex++;
}
return tLen - tIndex;
}
public static void Main()
{
string s = "coaching";
string t = "coding";
Console.WriteLine(MinCharsToAppend(s, t));
}
}
This C# solution uses a similar logical approach with two iterators advancing through the strings. It calculates how many characters of t
remain unmatched after scanning through s
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.
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
.
1#include <iostream>
2#include <vector>
3#include <string>
4
5using namespace std;
6
7void buildNext(const string &s, vector<vector<int>> &next) {
8 int sLen = s.length();
9 vector<int> last(26, sLen);
10 for (int i = sLen - 1; i >= 0; --i) {
11 last[s[i] - 'a'] = i;
12 for (int j = 0; j < 26; ++j) {
13 next[i][j] = last[j];
14 }
15 }
16}
17
18int minCharsToAppend(const string &s, const string &t) {
19 int sLen = s.length();
20 int tLen = t.length();
21 vector<vector<int>> next(sLen + 1, vector<int>(26, sLen));
22 buildNext(s, next);
23 int pos = 0, appendCount = 0;
24 for (int i = 0; i < tLen; ++i) {
25 if (next[pos][t[i] - 'a'] == sLen) {
26 appendCount += (tLen - i);
27 break;
28 }
29 pos = next[pos][t[i] - 'a'] + 1;
30 }
31 return appendCount;
32}
33
34int main() {
35 string s = "coaching";
36 string t = "coding";
37 cout << minCharsToAppend(s, t) << endl;
38 return 0;
39}
The C++ solution uses a similar dynamic programming approach by first building next occurrence positions with a 2D array. Then, it follows this data to calculate the minimum characters required to append.