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 INPUT phrases array: [0]: "writing code" First: "writing" Last: "code" [1]: "code review" First: "code" Last: "review" Hash Maps Built: firstWordMap "writing" --> [0] "code" --> [1] lastWordMap "code" --> [0] "review" --> [1] ALGORITHM STEPS 1 Build Hash Maps Map first/last words to indices 2 Find Matches Last word == First word? Check: phrases[0] + phrases[1] "code" == "code" MATCH! 3 Merge Phrases Combine at matching word "writing code" + "code review" "writing code review" 4 Sort and Dedupe Return sorted unique results FINAL RESULT Generated Puzzles: writing code code review writing code review Output Array: ["writing code review"] 1 puzzle found OK - Sorted! Key Insight: Use hash maps to efficiently find matching phrases. Map each phrase's first word and last word to its index. For each phrase, look up its last word in the firstWordMap to find mergeable phrases. Time: O(n^2 * m) where n = phrases count, m = avg phrase length. Use Set for deduplication. TutorialsPoint - Before and After Puzzle | Hash Map Optimization
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