
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 <stdio.h>
2#include <string.h>
3
4#define MAX_CHAR 26
5#define MAX_LEN 100000
6
7void buildNext(char *s, int next[][MAX_CHAR], int len) {
8 int last[MAX_CHAR];
9 for (int i = 0; i < MAX_CHAR; ++i) last[i] = len;
10 for (int i = len - 1; i >= 0; --i) {
11 last[s[i] - 'a'] = i;
12 for (int j = 0; j < MAX_CHAR; ++j) {
13 next[i][j] = last[j];
14 }
15 }
16}
17
18int minCharsToAppend(char *s, char *t) {
19 int sLen = strlen(s);
20 int tLen = strlen(t);
21 int next[MAX_LEN + 1][MAX_CHAR];
22 buildNext(s, next, sLen);
23 int appendCount = 0, pos = 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 char s[] = "coaching";
36 char t[] = "coding";
37 printf("%d\n", minCharsToAppend(s, t));
38 return 0;
39}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.