class Solution(object):
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
d, res = dict([(w[::-1], i) for i, w in enumerate(words)]), []
for i, w in enumerate(words):
for j in xrange(len(w)+1):
prefix, postfix = w[:j], w[j:]
if prefix in d and i != d[prefix] and postfix == postfix[::-1]:
res.append([i, d[prefix]])
if j>0 and postfix in d and i != d[postfix] and prefix == prefix[::-1]:
res.append([d[postfix], i])
return res
https://leetcode.com/submissions/detail/105553446/
def palindromePairs(self, words):
"""
:type words: List[str]
:rtype: List[List[int]]
"""
d, res = dict([(w[::-1], i) for i, w in enumerate(words)]), []
for i, w in enumerate(words):
for j in xrange(len(w)+1):
prefix, postfix = w[:j], w[j:]
if prefix in d and i != d[prefix] and postfix == postfix[::-1]:
res.append([i, d[prefix]])
if j>0 and postfix in d and i != d[postfix] and prefix == prefix[::-1]:
res.append([d[postfix], i])
return res
https://leetcode.com/submissions/detail/105553446/