00:00

Problem Statement

Input/Output Examples

Visual Explanation

If the prefix is a palindrome, then reverse(suffix) + word is a palindrome; if the suffix is a palindrome, then word + reverse(prefix) is one. A hash map turns each check into a single lookup.

Approaches

The map-based approach checks each word's splits. The brute force checks every ordered pair of words.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of words and k the maximum word length. The map approach checks k + 1 splits per word with O(k) work each, which beats checking all n² pairs when there are many short words.

Interview Tips and Recommended Approach

  • Handle the empty string. It pairs with every palindrome in both orders.
  • Avoid duplicate pairs. Make sure a split where both rules apply is not counted twice.
  • Explain the two directions. A palindromic prefix gives a partner in front; a palindromic suffix gives a partner behind.
  • Mention the trie variant. A trie of reversed words gives the same complexity with different trade-offs.

Recommended Approach: HashMap of Words

Why: it replaces the quadratic pair check with hash lookups per split, which scales much better when the list is long and the words are short.