Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def palindromePairs(self, words: List[str]) -> List[List[int]]:
- def is_palindrome(word):
- return word == word[::-1]
- word_dict = {word: i for i, word in enumerate(words)}
- result = []
- for i, word in enumerate(words):
- for j in range(len(word) + 1):
- prefix = word[:j]
- suffix = word[j:]
- if is_palindrome(prefix):
- reversed_suffix = suffix[::-1]
- if reversed_suffix != word and reversed_suffix in word_dict:
- result.append([word_dict[reversed_suffix], i])
- if j != len(word) and is_palindrome(suffix):
- reversed_prefix = prefix[::-1]
- if reversed_prefix != word and reversed_prefix in word_dict:
- result.append([i, word_dict[reversed_prefix]])
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement