FastPrepFastPrep
Problem Brief

Lost Messages

FULLTIMENEW 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

1Example 1

Input
sent = "I am a programmer", received = "I programmer"
Output
["am", "a"]
Explanation
The words "am" and "a" appear in sent but are missing from received, so they are returned in order.

2Example 2

Input
sent = "we love coding daily", received = "we coding"
Output
["love", "daily"]
Explanation
The recovered message keeps "we" and "coding" in order, so the missing words are "love" and "daily".

3Example 3

Input
sent = "alpha beta gamma delta", received = "alpha delta"
Output
["beta", "gamma"]
Explanation
The recovered message is a subsequence of the sent message. The missing words between the matched endpoints are returned.

Constraints

Limits and guarantees your solution can rely on.

  • 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.
public String[] lostFragments(String sent, String received) {
  // write your code here
}
Input

sent

"I am a programmer"

received

"I programmer"

Output

["am", "a"]

Sign in to submit your solution.