Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let <code>dp[i]</code> be the longest suffix of <code>word2</code> that exists as a subsequence of suffix of the substring of <code>word1</code> starting at index <code>i</code>.
If <code>dp[i + 1] < m</code> and <code>word1[i] == word2[m - dp[i + 1] - 1]</code>,<code>dp[i] = dp[i + 1] + 1</code>. Otherwise, <code>dp[i] = dp[i + 1]</code>.
For each index <code>i</code>, greedily select characters using the <code>dp</code> array to know whether a solution exists.