Skip to content
DeepForgeby svx
All problems
HardAlgorithmsal-164

Palindrome Pairs Count

Problem

Given a list of words, count ordered pairs (i, j) with i != j such that words[i] + words[j] is a palindrome.

For small inputs, test every ordered pair and check whether the concatenation reads the same forwards and backwards. A single-element list contains no pairs.

Hint

The pair is ordered and the two indices must differ, even when both words are empty.

Test cases

case 1
in: [["abcd","dcba","lls","s","sssll"]]
expected: 4
case 2
in: [["bat","tab","cat"]]
expected: 2
case 3
in: [["a",""]]
expected: 2
case 4
in: [["abc","cba","bca"]]
expected: 2
case 5
in: [[""]]
expected: 0
Starter codePython
def count_palindrome_pairs(words):
    # Your code here
    pass

5 test cases · run your code against them in the editor