#446 · Strings · Expert · 220 XP · about 45 min
A palindrome reads the same forwards and backwards, like "racecar". Given a list of different words, write palindrome_pairs(words) that finds every pair of positions (i, j) with i != j where words[i] + words[j] is a palindrome.
Return the pairs as a list of tuples (i, j), sorted in increasing order (by i, then by j).
palindrome_pairs(['bat', 'tab', 'cat']) # returns [(0, 1), (1, 0)] palindrome_pairs(['abcd', 'dcba', 'lls', 's', 'sssll']) # returns [(0, 1), (1, 0), (2, 4), (3, 2)] palindrome_pairs(['a', '']) # returns [(0, 1), (1, 0)]
In the second example "lls" + "sssll" gives "llssssll", and "s" + "lls" gives "slls". The empty string pairs both ways with every word that is a palindrome on its own.
0 <= len(words) <= 5,000, and all words are different.First solves only, ranked by stars and then time.