Why my python code cause Time Limit Exceeded?

  • 0
    class Solution(object):
    def palindromePairs(self, words):
        :type words: List[str]
        :rtype: List[List[int]]
        result = []
        for i in range(len(words)):
            for j in range(len(words)):
                if i == j:
                combine = words[i]+words[j]
                if self.decide(combine):
                    result.append([i, j])
        return result
    #decide if it is palindrome
    def decide(self, b):
        for i in range(len(b)/2):
            if b[i]!=b[len(b)-i-1]:
                return True

    I try it on my computer on the last instance(which is a long string list) but it runs so well.

  • 0

    I think naive way has a very high time complexity, combine is also O(n)

  • 0

    it is the worst way(brute force)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.