Magicsheet logo

Append Characters to String to Make Subsequence

Medium
25%
Updated 8/1/2025

Append Characters to String to Make Subsequence

What is this problem about?

The "Append Characters to String to Make Subsequence interview question" is a string matching challenge. You are given two strings, ss and tt. Your goal is to find the minimum number of characters you need to append to the end of ss so that tt becomes a subsequence of ss. A subsequence is a string derived by deleting zero or more characters from the original string without changing the order of the remaining characters.

Why is this asked in interviews?

Companies like Microsoft and Amazon ask the "Append Characters to String to Make Subsequence coding problem" to test a candidate's understanding of string traversal and the "Two Pointers interview pattern." It's an efficient way to evaluate if a candidate can find the longest prefix of tt that already exists as a subsequence in ss.

Algorithmic pattern used

This problem is solved using the Two Pointers and Greedy patterns.

  1. Pointer 1: Start at the beginning of string ss.
  2. Pointer 2: Start at the beginning of string tt.
  3. Traversal: Iterate through ss. Whenever the character at s[p1] matches the character at t[p2], increment p2. Always increment p1.
  4. Conclusion: After traversing ss, the value of p2 tells you how many characters of tt are already present as a subsequence. The number of characters to append is the remaining length of tt: length(t) - p2.

Example explanation

String ss: "abcde", String tt: "acegh"

  1. Pointer p2 at 'a'. Find 'a' in ss. Match! p2 moves to 'c'.
  2. Find 'c' in ss. Match! p2 moves to 'e'.
  3. Find 'e' in ss. Match! p2 moves to 'g'.
  4. Continue through ss. No 'g' found.
  5. p2 stops after matching 3 characters ("ace"). Length of tt is 5. Characters to append: 53=25 - 3 = 2 (the "gh" part).

Common mistakes candidates make

  • Confusing Subsequence with Substring: Trying to find a continuous block of characters instead of allowing gaps.
  • Over-complicating with DP: Attempting a complex Dynamic Programming approach (like Longest Common Subsequence) when a simple linear scan is faster and sufficient.
  • Wrong Append Location: Forgetting that characters can only be added to the end of ss, which simplifies the problem significantly.

Interview preparation tip

Whenever a problem involves "subsequences" and "minimum characters," first check if a greedy approach with two pointers works. It's almost always O(N)O(N) and much more efficient than DP.

Similar Questions