Z15

Palindrome Pairs

Staff / Principal Engineer
Target Time: 40 - 50 Minutes
Type: ARRAY

Problem Statement & Objective

Given a list of unique words, return all pairs of distinct indices (i, j) such that the concatenation of the two words, words[i] + words[j], forms a palindrome.

Function Signature (Python)

palindrome_pairs(words: list[str]) -> list[list[int]]

Input & Output Contract

Output Rule:List of index pairs [[i, j], ...].

System Constraints

1 <= words.length <= 5000
0 <= words[i].length <= 300
words[i] consists of lowercase English letters

Example Execution Walkthrough

words = ["abcd","dcba","lls","s","sssll"]
"abcd" + "dcba" = "abcddcba" (palindrome) -> [0, 1]
"dcba" + "abcd" = "dcbaabcd" (palindrome) -> [1, 0]
result: [[0,1],[1,0],[3,2],[2,4]]

Example Test Scenarios (3)

Example 1: Klasik Palindrom Çiftleri
Input:[["abcd","dcba","lls","s","sssll"]]
Expected Output:[[0,1],[1,0],[2,4],[3,2]]
Example 2: Tam Ters Çiftler
Input:[["bat","tab","cat"]]
Expected Output:[[0,1],[1,0]]
Example 3: Boş Kelime İçeren
Input:[["a",""]]
Expected Output:[[0,1],[1,0]]
Loading Python Editor...
Input Parameters
[
  [
    "abcd",
    "dcba",
    "lls",
    "s",
    "sssll"
  ]
]
Expected Output
[
  [
    0,
    1
  ],
  [
    1,
    0
  ],
  [
    2,
    4
  ],
  [
    3,
    2
  ]
]