Before and After Puzzle - Problem

Given a list of phrases, generate a list of Before and After puzzles.

A phrase is a string that consists of lowercase English letters and spaces only. No space appears in the start or the end of a phrase. There are no consecutive spaces in a phrase.

Before and After puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. Note that only the last word of the first phrase and the first word of the second phrase are merged in this process.

Return the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] where i != j. Note that the order of matching two phrases matters, we want to consider both orders.

You should return a list of distinct strings sorted lexicographically, after removing all duplicate phrases in the generated Before and After puzzles.

Input & Output

Example 1 — Basic Connection
$ Input: phrases = ["writing code", "code review"]
Output: ["writing code review"]
💡 Note: The last word "code" of first phrase matches the first word "code" of second phrase. Merge by removing the duplicate: "writing code" + "review" = "writing code review"
Example 2 — Multiple Connections
$ Input: phrases = ["mission statement", "a quick", "quick brown", "brown fox", "fox jumps"]
Output: ["a quick brown fox", "a quick brown fox jumps", "quick brown fox jumps"]
💡 Note: Multiple chains possible: "a quick" → "quick brown" → "brown fox" → "fox jumps" creates several valid before-and-after puzzles
Example 3 — No Connections
$ Input: phrases = ["hello world", "good morning", "nice day"]
Output: []
💡 Note: No phrase's last word matches any other phrase's first word, so no before-and-after puzzles can be formed

Constraints

  • 1 ≤ phrases.length ≤ 100
  • 1 ≤ phrases[i].length ≤ 100
  • phrases[i] consists of lowercase English letters and spaces
  • No phrase has leading or trailing spaces
  • No phrase has consecutive spaces

Visualization

Tap to expand
Before and After Puzzle: Connect Phrases Through Matching Words"writing code""code review""review meeting"Step 1: Identify First and Last WordswritingcodefirstlastcodereviewStep 2: Find Matching Wordsmatch!Step 3: Merge by Removing Duplicate"writing code" + "review" (skip duplicate "code")Output: ["writing code review"]Before and After puzzle successfully created!
Understanding the Visualization
1
Input Phrases
Array of phrases with first and last words
2
Find Connections
Match last word of one phrase with first word of another
3
Merge Phrases
Combine phrases by removing duplicate connecting word
Key Takeaway
🎯 Key Insight: Use hash maps to efficiently match last words with first words instead of checking every pair
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~25 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen