Problem · Two Pointers

Lost Messages

EasySuperhumanFULLTIMENEW GRADOA

A message is represented as a string of space-delimited words.

You are given two strings:

  • sent: the original message
  • received: the message that was recovered after transmission

Some words may be missing from received. It is guaranteed that all words in received appear in the same relative order as they do in sent.

Return all words that appear in sent but do not appear in received, preserving their original order.

Function Description

Complete the function lostFragments in the editor below.

lostFragments has the following parameters:

  • String sent: the original message that was sent
  • String received: the recovered message after transmission

Returns

String[]: the words from sent that are missing from received, in the order they appear in sent

Examples
01 · Example 1
sent = "I am a programmer"
received = "I programmer"
return = ["am", "a"]
The words "am" and "a" appear in sent but are missing from received, so they are returned in order.
02 · Example 2
sent = "we love coding daily"
received = "we coding"
return = ["love", "daily"]
The recovered message keeps "we" and "coding" in order, so the missing words are "love" and "daily".
03 · Example 3
sent = "alpha beta gamma delta"
received = "alpha delta"
return = ["beta", "gamma"]
The recovered message is a subsequence of the sent message. The missing words between the matched endpoints are returned.
Constraints
  • sent and received contain only lowercase or uppercase English letters and spaces.
  • Words are separated by single spaces.
  • 1 ≤ received.length() < sent.length() ≤ 10^5
  • The length of any individual word in sent or received is at most 15.
  • received is guaranteed to be a subsequence of sent at the word level.
More Superhuman problems
drafts saved locally
public String[] lostFragments(String sent, String received) {
  // write your code here
}
sent"I am a programmer"
received"I programmer"
expected["am", "a"]
checking account