1. 程式人生 > >【LeetCode】336. Palindrome Pairs 解題報告(Python)

【LeetCode】336. Palindrome Pairs 解題報告(Python)

目錄

題目描述

Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.

Example 1:

Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]] 
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]

Example 2:

Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]] 
Explanation: The palindromes are ["battab","tabbat"]

題目大意

如果從input進來的字串中選取兩個拼接在一起能構成迴文字串,那麼就把這兩個的索引加入到結果中。返回所有的索引列表。

解題方法

HashTable

這個題暴力求解會超時,優秀的解法還真不是容易想出來。不愧是Hard題啊,這個也是我做的第600個題。我就照搬大神的解法了[LeetCode]Palindrome Pairs

O(k * n ^2)解法 其中k為單詞個數,n為單詞的長度:

利用字典wmap儲存單詞 -> 下標的鍵值對

遍歷單詞列表words,記當前單詞為word,下標為idx:

1). 若當前單詞word本身為迴文,且words中存在空串,則將空串下標bidx與idx加入答案

2). 若當前單詞的逆序串在words中,則將逆序串下標ridx與idx加入答案

3). 將當前單詞word拆分為左右兩半left,right。

     3.1) 若left為迴文,並且right的逆序串在words中,則將right的逆序串下標rridx與idx加入答案
     
     3.2) 若right為迴文,並且left的逆序串在words中,則將left的逆序串下標idx與rlidx加入答案

時間複雜度是O(k * n ^2),空間複雜度是O(kN).

class Solution(object):
    def palindromePairs(self, words):
        """
        :type words: List[str]
        :rtype: List[List[int]]
        """
        wmap = {w : i for i, w in enumerate(words)}
        
        def isPalindrome(word):
            _len = len(word)
            for x in range(_len / 2):
                if word[x] != word[_len - x - 1]:
                    return False
            return True
        
        res = set()
        for idx, word in enumerate(words):
            if word and isPalindrome(word) and "" in wmap:
                nidx = wmap[""]
                res.add((idx, nidx))
                res.add((nidx, idx))
            
            rword = word[::-1]
            if word and rword in wmap:
                nidx = wmap[rword]
                if idx != nidx:
                    res.add((idx, nidx))
                    res.add((nidx, idx))
            
            for x in range(1, len(word)):
                left, right = word[:x], word[x:]
                rleft, rright = left[::-1], right[::-1]
                if isPalindrome(left) and rright in wmap:
                    res.add((wmap[rright], idx))
                if isPalindrome(right) and rleft in wmap:
                    res.add((idx, wmap[rleft]))
        return list(res)

相似題目

參考資料

日期

2018 年 11 月 1 日 —— 小光棍節