LeetCode sur la plus longue sous-chaîne palindromique en Python

Nov 01 2020

Ceci est une question de programmation de LeetCode :

Étant donné une chaîne s, renvoie la plus longue sous-chaîne palindromique de s.

Exemple 1:

Entrée: s = "babad" Sortie: "bab" Remarque: "aba" est également une réponse valide.

Voici mon code qui échoue à l'entrée suivante en raison de "Time Limit Exceeded":

""

class Solution(object):

    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s) == 0:
            return None
        if len(s) == 1:
            return s

        P = [[False]*len(s) for i in range(len(s))]

        for i in range(len(s)):
            P[i][i]   = True

        for i in range(len(s)-1):
            P[i][i+1] = (s[i]==s[i+1])

        for s_len in range(3,len(s)+1):
            for i in range(len(s)+1-s_len):
                P[i][i+s_len-1] = P[i+1][i+s_len-2] and (s[i]==s[i+s_len-1])

        ip = 0
        jp = 0
        max_len = 1

        for i in range(len(s)):
            for j in range(len(s)):
                if P[i][j] and j+1-i > max_len:
                    max_len = j+1-i
                    ip = i
                    jp = j 
                    continue

        return s[ip:jp+1]

J'essayais de suivre l'approche suivante décrite dans la solution du site. Quelqu'un pourrait-il m'aider à voir comment rendre mon code plus efficace?


Réponses

6 Emma Nov 01 2020 at 08:36

Avis de non-responsabilité: pas un réviseur de code

Voici cependant quelques brefs commentaires:

  • Vous faites une boucle deux fois.
  • Cela ferait de la force brute.
  • La force brute échoue généralement pour certaines questions moyennes et difficiles sur LeetCode.

Solution alternative

  • Ici, nous ferions une boucle une fois:
class Solution:
    def longestPalindrome(self, s):
        if len(s) < 1:
            return s

        def isPalindrome(left, right):
            return s[left:right] == s[left:right][::-1]

        left, right = 0, 1
        for index in range(1, len(s)):
            if index - right > 0 and isPalindrome(index - right - 1, index + 1):
                left, right = index - right - 1, right + 2
            if index - right >= 0 and isPalindrome(index - right, index + 1):
                left, right = index - right, right + 1
        return s[left: left + right]

Votre solution

  • Je viens de tester votre solution (passe marginalement):
class Solution(object):

    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s) < 1:
            return s

        P = [[False] * len(s) for i in range(len(s))]

        for i in range(len(s)):
            P[i][i] = True

        for i in range(len(s) - 1):
            P[i][i + 1] = (s[i] == s[i + 1])

        for s_len in range(3, len(s) + 1):
            for i in range(len(s) + 1 - s_len):
                P[i][i + s_len - 1] = P[i + 1][i + s_len - 2] and (s[i] == s[i + s_len - 1])

        ip = 0
        jp = 0
        max_len = 1

        for i in range(len(s)):
            for j in range(len(s)):
                if P[i][j] and j + 1 - i > max_len:
                    max_len = j + 1 - i
                    ip = i
                    jp = j
                    continue

        return s[ip:jp + 1]

  • Puisque les temps d'exécution sont élevés, il est possible que cela échoue parfois.

  • Je suppose que LeetCode a une limite de temps pour chaque problème, peut-être 10 secondes serait la limite pour ce problème spécifique.

  • Probablement basé sur la géolocalisation / l'heure, le runtime serait également différent.


Encore un peu d'optimisation:

  • S'il vous plaît voir cette ligne for j in range(i + 1, len(s))::
class Solution(object):

    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s) < 1:
            return s

        P = [[False] * len(s) for _ in range(len(s))]

        for i in range(len(s)):
            P[i][i] = True

        for i in range(len(s) - 1):
            P[i][i + 1] = (s[i] == s[i + 1])

        for s_len in range(3, len(s) + 1):
            for i in range(len(s) + 1 - s_len):
                P[i][i + s_len - 1] = P[i + 1][i + s_len - 2] and (s[i] == s[i + s_len - 1])

        ip = 0
        jp = 0
        max_len = 1

        for i in range(len(s)):
            for j in range(i + 1, len(s)):
                if P[i][j] and j + 1 - i > max_len:
                    max_len = j + 1 - i
                    ip = i
                    jp = j
                    continue

        return s[ip:jp + 1]

  • Cela réduit d'environ 1 seconde mais toujours pas bon.

  • Je suis sûr qu'il existe d'autres moyens d'optimiser.

  • Attends un peu! Il y a de bons critiques Python ici. Vous aiderait probablement.


Avec quelques commentaires:

class Solution:
    def longestPalindrome(self, s):
        if len(s) < 1:
            return s

        def isPalindrome(left, right):
            return s[left:right] == s[left:right][::-1]

        # We set the left pointer on the first index
        # We set the right pointer on the second index
        # That's the minimum true palindrome
        left, right = 0, 1

        # We visit the alphabets from the second index forward once
        for index in range(1, len(s)):
            # Here we move the right pointer twice and once checking for palindromeness
            # We boundary check using index - right, to remain positive
            if index - right > 0 and isPalindrome(index - right - 1, index + 1):
                print(f"Step {index - 1}: Left pointer is at {index - right - 1} and Right pointer is at {index + 1}")
                print(f"Palindromeness start: {index - right - 1} - Palindromeness end: {index + 1}")
                print(f"Window length: {right}")
                print(f"Before: Left is {left} and Right is {left + right}")
                left, right = index - right - 1, right + 2
                print(f"After: Left is {left} and Right is {left + right}")
                print(f"String: {s[left: left + right]}")
                print('#' * 50)
            if index - right >= 0 and isPalindrome(index - right, index + 1):
                print(f"Step {index - 1}: Left pointer is at {index - right} and Right pointer is at {index + 1}")
                print(f"Palindromeness start: {index - right - 1} - Palindromeness end: {index + 1}")
                print(f"Window length: {right + 1}")
                print(f"Before: Left is {left} and Right is {left + right}")
                left, right = index - right, right + 1
                print(f"After: Left is {left} and Right is {left + right}")
                print(f"String: {s[left: left + right]}")
                print('#' * 50)
        return s[left: left + right]


Solution().longestPalindrome("glwhcebdjbdroiurzfxxrbhzibilmcfasshhtyngwrsnbdpzgjphujzuawbebyhvxfhtoozcitaqibvvowyluvdbvoqikgojxcefzpdgahujuxpiclrrmalncdrotsgkpnfyujgvmhydrzdpiudkfchtklsaprptkzhwxsgafsvkahkbsighlyhjvbburdfjdfvjbaiivqxdqwivsjzztzkzygcsyxlvvwlckbsmvwjvrhvqfewjxgefeowfhrcturolvfgxilqdqvitbcebuooclugypurlsbdfquzsqngbscqwlrdpxeahricvtfqpnrfwbyjvahrtosovsbzhxtutyfjwjbpkfujeoueykmbcjtluuxvmffwgqjgrtsxtdimsescgahnudmsmyfijtfrcbkibbypenxnpiozzrnljazjgrftitldcueswqitrcvjzvlhionutppppzxoepvtzhkzjetpfqsuirdcyqfjsqhdewswldawhdyijhpqtrwgyfmmyhhkrafisicstqxokdmynnnqxaekzcgygsuzfiguujyxowqdfylesbzhnpznayzlinerzdqjrylyfzndgqokovabhzuskwozuxcsmyclvfwkbimhkdmjacesnvorrrvdwcgfewchbsyzrkktsjxgyybgwbvktvxyurufsrdufcunnfswqddukqrxyrueienhccpeuqbkbumlpxnudmwqdkzvsqsozkifpznwapxaxdclxjxuciyulsbxvwdoiolgxkhlrytiwrpvtjdwsssahupoyyjveedgqsthefdyxvjweaimadykubntfqcpbjyqbtnunuxzyytxfedrycsdhkfymaykeubowvkszzwmbbjezrphqildkmllskfawmcohdqalgccffxursvbyikjoglnillapcbcjuhaxukfhalcslemluvornmijbeawxzokgnlzugxkshrpojrwaasgfmjvkghpdyxt")

Impressions:

Step 18: Left pointer is at 18 and Right pointer is at 20
Palindromeness start: 17 - Palindromeness end: 20
Window length: 2
Before: Left is 0 and Right is 1
After: Left is 18 and Right is 20
String: xx
##################################################
Step 25: Left pointer is at 24 and Right pointer is at 27
Palindromeness start: 23 - Palindromeness end: 27
Window length: 3
Before: Left is 18 and Right is 20
After: Left is 24 and Right is 27
String: ibi
##################################################
Step 462: Left pointer is at 460 and Right pointer is at 464
Palindromeness start: 459 - Palindromeness end: 464
Window length: 4
Before: Left is 24 and Right is 27
After: Left is 460 and Right is 464
String: pppp
##################################################

Bon codage! (ˆ_ˆ)