words. Find every ordered pair of indices [i, j] with i != j such that gluing words[i] + words[j] gives a string that reads the same forwards and backwards.
Order inside a pair matters: [i, j] and [j, i] are different glued strings, and each one is included only if it is itself a palindrome. The pairs themselves may be returned in any order.
Checking every pair takes O(n² · L). Aim for a solution whose running time depends on the total length of the words rather than on the number of words squared.words=['ten', 'net', 'noon', 'on'] → [[0, 1], [1, 0]]
"ten" + "net" = "tennet" and "net" + "ten" = "netten" both read the same backwards. "noon" and "on" do not combine into a palindrome in either order, so the answer is [0, 1] and [1, 0].
words=['lev', 'el', 'ab', 'xyzba'] → [[0, 1]]
"lev" + "el" = "level" is a palindrome, giving [0, 1]. Neither "abxyzba" nor "xyzbaab" is one, so [[0, 1]] is the whole answer.
words=['', 'aba', 'c'] → [[1, 0], [0, 1], [2, 0], [0, 2]]
The empty word can be glued to any palindrome on either side: [0, 1], [1, 0], [0, 2] and [2, 0].
words=['ten', 'net', 'noon', 'on']
[[0, 1], [1, 0]]
Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.