-
Notifications
You must be signed in to change notification settings - Fork 0
/
0336.py
33 lines (28 loc) · 1.18 KB
/
0336.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def palindromePairs(self, words: List[str]) -> List[List[int]]:
def isPalindrome(word):
# return word[::-1] == word
len_ = len(word)
for x in range(len_//2):
if word[x] != word[len_-x-1]:
return False
return True
res = set()
table = collections.defaultdict(list)
for i, w in enumerate(words):
table[w] = i
for i, w in enumerate(words):
if isPalindrome(w) and "" in table and i != table[""]:
res.add((i, table[""]))
res.add((table[""], i))
if w[::-1] in table and table[w[::-1]] != i:
res.add((i, table[w[::-1]]))
res.add((table[w[::-1]], i))
for x in range(1, len(w)):
left, right = w[:x], w[x:]
rleft, rright = left[::-1], right[::-1]
if isPalindrome(left) and rright in table:
res.add((table[rright], i))
if isPalindrome(right) and rleft in table:
res.add((i, table[rleft]))
return res