Before and After Puzzle Problem
Description
LeetCode Problem 1181.
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.
Example 1:
Input: phrases = ["writing code","code rocks"]
Output: ["writing code rocks"]
Example 2:
Input: phrases = ["mission statement","a quick bite to eat","a chip off the old block","chocolate bar","mission impossible","a man on a mission","block party","eat my words","bar of soap"]
Output: ["a chip off the old block party","a man on a mission impossible","a man on a mission statement","a quick bite to eat my words","chocolate bar of soap"]
Example 3:
Input: phrases = ["a","b","a"]
Output: ["a"]
Example 4:
Input: phrases = ["ab ba","ba ab","ab ba"]
Output: ["ab ba ab","ba ab ba"]
Constraints:
1 <= phrases.length <= 1001 <= phrases[i].length <= 100
Solution
Approach: Try Every Ordered Pair
With at most 100 phrases there are fewer than 104 ordered pairs, so we can simply try them all.
First record each phrase's first word and last word (for a one-word phrase, both are the phrase itself). Then for every pair (i, j) with i != j and last[i] == first[j], the puzzle is phrase i followed by phrase j with its first word cut off: the shared word must appear only once. Cutting exactly len(first[j]) characters keeps the space before phrase j's second word, and leaves nothing when phrase j is a single word.
Two details are easy to miss:
- The order of a pair matters, so
(i, j)and(j, i)are both tried. - Equal phrases at different indices still form a pair (
["a", "b", "a"]gives"a"), and different pairs can produce the same puzzle, so collect the results in a set before sorting them.
Complexity: O(n2 * m) time to build the puzzles plus the cost of sorting them, where n is the number of phrases and m their length, and O(n2 * m) space for the results.
class Solution:
def beforeAndAfterPuzzles(self, phrases: List[str]) -> List[str]:
firsts = [p.split(" ")[0] for p in phrases]
lasts = [p.split(" ")[-1] for p in phrases]
puzzles = set() # a set drops the duplicates
for i in range(len(phrases)):
for j in range(len(phrases)):
if i != j and lasts[i] == firsts[j]:
# Phrase j without its first word, which phrase i already ends with.
puzzles.add(phrases[i] + phrases[j][len(firsts[j]):])
return sorted(puzzles)