← Pinterest Interview Insights
The deletion cost is what makes this non-trivial.
Model the problem as finding the minimum number of copies of string a such that b is a subsequence of the concatenated copies, then account for deletions. Use dynamic programming or greedy matching to compute the minimum copies, and discuss time/space complexity and edge cases.
Pro tip: Clarify that deletions are free and only copies count, so the problem reduces to finding the smallest k where b is a subsequence of a repeated k times. This simplifies the solution and shows you understand the core constraint.
Restate the problem: we can append full copies of a and delete any characters; we need the minimum number of copies to obtain b. Note that deletions are unlimited and free, so the challenge is to match b as a subsequence of repeated a.
Handle b empty (answer 0), a empty (impossible unless b empty), and check if every character in b appears in a; if not, return -1. Also consider if b is already a subsequence of a (answer 1).
Use a greedy two-pointer approach: iterate through b, and for each character, find its next occurrence in a (wrapping around and incrementing copy count when needed). Alternatively, use DP to compute the minimum copies, but greedy is optimal here.
The greedy approach runs in O(|b| * log |a|) with preprocessed positions, or O(|b| * |a|) naive; space O(|a|) for positions. Discuss trade-offs and potential optimizations.
Walk through examples like a='abc', b='abcabc' (2 copies), a='abc', b='ac' (1 copy), and a='abc', b='abd' (impossible). Mention that the greedy approach is optimal and compare with DP if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.