2dbi

Missing Words

viaLeetCode

Problem Given an original sentence S and a sentence T that is a subsequence of S's words (same order, some words omitted), return the words of S missing from T, in order.

Input / Output

  • Input: strings S and T (space-separated words).
  • Output: list of missing words in their order of appearance in S.

Constraints

  • Up to 10^5 words; O(|S| + |T|) expected. Duplicate words must be matched by position, not by value.

Example

  • S = "I am practicing daily to improve programming", T = "am daily to improve" → ["I", "practicing", "programming"]

Expected approach

  • Split both into word arrays and walk two pointers: advance j into T only when S[i] == T[j]; otherwise record S[i] as missing. Because T is a guaranteed subsequence, greedy matching is correct. O(n) time, O(1) extra space beyond the output.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account