Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 9. 17. 23:45
https://leetcode.com/problems/palindrome-pairs/
Palindrome Pairs - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Hard 난이도긴 한데.. n이 그렇게 크지 않아서 여러 번 loop 돌려서 처리 가능
class Solution:
def palindromePairs(self, words: List[str]) -> List[List[int]]:
backward, res = {}, []
for i, word in enumerate(words):
backward[word[::-1]] = i
for i, word in enumerate(words):
if word in backward and backward[word] != i:
res.append([i, backward[word]])
if word != "" and "" in backward and word == word[::-1]:
res.append([i, backward[""]])
res.append([backward[""], i])
for j in range(len(word)):
if word[j:] in backward and word[:j] == word[j-1::-1]:
res.append([backward[word[j:]], i])
if word[:j] in backward and word[j:] == word[:j-1:-1]:
res.append([i, backward[word[:j]]])
return res
반응형