/336. Palindrome Pairs

336. Palindrome Pairs

Hard
Arrays37% acceptance

Given a 0-indexed array of unique strings string_list, return all pairs of indices (i, j) such that i != j, 0 <= i, j < len(string_list), and the concatenation string_list[i] + string_list[j] forms a palindrome. The output should be a list of all such pairs.

Example 1

Input: [race, car, ecar, ace]

Output: [[0,2],[2,0],[1,3],[3,1]]

Explanation: Pairs (0,2) and (2,0) form racecar and ecarrace, both palindromes. Pairs (1,3) and (3,1) form carace and acecar, both palindromes.

Example 2

Input: [mad, dam, add, dad]

Output: [[0,1],[1,0],[2,3],[3,2]]

Explanation: Pairs (0,1) and (1,0) form maddam and dammad, both palindromes. Pairs (2,3) and (3,2) form adddad and dadadd, both palindromes.

Constraints

  • 1 <= len(string_list) <= 5000
  • 0 <= len(string_list[i]) <= 300
  • Each string in string_list consists only of lowercase English letters
  • All strings in string_list are unique
Python (current runtime)

Case 1

Input: ['noon', 'no', 'on', 'n']

Expected: [[1,2],[2,1],[0,3],[3,0]]

Case 2

Input: ['abc', 'cba', 'bca', 'acb']

Expected: [[0,1],[1,0]]

Case 3

Input: ['x', '', 'y']

Expected: [[0,1],[1,0],[2,1],[1,2]]

Case 4

Input: ['level', 'eve', 'l']

Expected: [[0,2],[2,0]]