Skip to content

Commit b5240c9

Browse files
Create Solution.py
1 parent 2edcffe commit b5240c9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Palindrome Pairs/Solution.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def palindromePairs(self, words: List[str]) -> List[List[int]]:
3+
ans = []
4+
dict = {word[::-1]: i for i, word in enumerate(words)}
5+
6+
for i, word in enumerate(words):
7+
if "" in dict and dict[""] != i and word == word[::-1]:
8+
ans.append([i, dict[""]])
9+
10+
for j in range(1, len(word) + 1):
11+
l = word[:j]
12+
r = word[j:]
13+
if l in dict and dict[l] != i and r == r[::-1]:
14+
ans.append([i, dict[l]])
15+
if r in dict and dict[r] != i and l == l[::-1]:
16+
ans.append([dict[r], i])
17+
18+
return ans

0 commit comments

Comments
 (0)