Palindrome Pairs

Problem: Palindrome Pairs

A brute-force solution.

Code in Python:

class Solution(object):
    def palindromePairs(self, words):
        """
        :type words: List[str]
        :rtype: List[List[int]]
        """
        dict = {}
        for i, word in enumerate(words):
            dict[word] = i

        res = []
        for i, word in enumerate(words):
            for j in xrange(len(word)-1, 0, -1):
                if word[j-1::-1] != word and word[j-1::-1] in dict and word+word[j-1::-1] == word[:j]+word[::-1]:
                    res.append([i, dict[word[j-1::-1]]])
            for j in xrange(len(word)):
                if word[j:][::-1] != word and word[j:][::-1] in dict and word[j:][::-1]+word == word[::-1]+word[j:]:
                    res.append([dict[word[j:][::-1]], i])
            if "" in dict and word == word[::-1] and word != "": res += [[i,dict[""]], [dict[""],i]]
        return res

results matching ""

    No results matching ""