LeetCode 1181: Before and After Puzzle Solution

Master LeetCode problem 1181 (Before and After Puzzle), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1181. Before and After Puzzle

Problem Explanation

Explanation:

The problem asks us to find all phrases that can be formed by taking the first word of one phrase and the last word of another phrase. We need to return the phrases in any order.

To solve this problem:

  1. Create a set to store all the unique first words.
  2. Create a map to store the last words for each phrase.
  3. Iterate through the phrases and update the set and map accordingly.
  4. For each phrase, check if its last word is present in the set. If it is, then add the combination of the current phrase and the corresponding last word to the result set.

Time Complexity:

The time complexity is O(n) where n is the number of phrases.

Space Complexity:

The space complexity is O(n) for storing the set and map.

:

Solution Code

import java.util.*;

class Solution {
    public List<String> beforeAndAfterPuzzles(String[] phrases) {
        Set<String> firstWords = new HashSet<>();
        Map<String, List<String>> lastWordsMap = new HashMap<>();
        Set<String> results = new HashSet<>();

        for (String phrase : phrases) {
            String[] words = phrase.split(" ");
            firstWords.add(words[0]);
            String lastWord = words[words.length - 1];
            if (!lastWordsMap.containsKey(lastWord)) {
                lastWordsMap.put(lastWord, new ArrayList<>());
            }
            lastWordsMap.get(lastWord).add(phrase);
        }

        for (String phrase : phrases) {
            String[] words = phrase.split(" ");
            String lastWord = words[words.length - 1];
            if (firstWords.contains(lastWord)) {
                for (String startPhrase : lastWordsMap.get(lastWord)) {
                    if (!startPhrase.equals(phrase)) {
                        results.add(startPhrase + phrase.substring(lastWord.length()));
                    }
                }
            }
        }

        return new ArrayList<>(results);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1181 (Before and After Puzzle)?

This page provides optimized solutions for LeetCode problem 1181 (Before and After Puzzle) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1181 (Before and After Puzzle)?

The time complexity for LeetCode 1181 (Before and After Puzzle) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1181 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1181 in Java, C++, or Python.

Back to LeetCode Solutions