25 · Tries

#336 Palindrome Pairs

Hardsolve on LeetCode ↗

frequently reported atAirbnbGoogleAmazon

the problem

In plain words

You get an array of distinct words made of lowercase letters (a word may be empty). Return every pair of indices [i, j] with i ≠ j such that words[i] followed by words[j] reads the same forwards and backwards. The pairs can be in any order.

in words = ["abcd","dcba","lls","s","sssll"]

out [[0,1],[1,0],[3,2],[2,4]]

"abcddcba", "dcbaabcd", "slls", "llssssll".

in words = ["bat","tab","cat"]

out [[0,1],[1,0]]

in words = ["a",""]

out [[0,1],[1,0]]

The empty word pairs with every palindrome, on both sides.

  • 1 ≤ words.length ≤ 5000
  • 0 ≤ words[i].length ≤ 300
  • Lowercase English letters; all words are distinct

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #125
      Valid Palindrome
      Easyin Two Pointers
    2. #5
      Longest Palindromic Substring
      Mediumin Dynamic Programming
    3. #214
      Shortest Palindrome
      Hardin Range Queries & Strings