Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Lines changed: 18 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /