Участник:DChulkov/Leet7

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/submissions/

class Solution:
    def minInsertions(self, s: str) -> int:
        p = [[0] * len(s) for _ in s]
        for j in range(1,len(s)): #j - the lenght of substring
            for i in range(len(s)):
                if i + j >= len(s):continue
                elif i + 1 > i + j - 1 and s[i] == s[i+j]:
                    continue
                elif i + 1 > i + j - 1 and s[i] != s[i + j]:
                    p[i][i + j] = 1
                elif s[i] == s[i+j]:
                    p[i][i+j] = min(p[i+1][i+j-1],p[i+1][i+j]+1,p[i][i+j-1] + 1)
                else:
                    p[i][i + j] = min(p[i + 1][i + j] + 1,p[i + 1][i + j-1] + 2, p[i][i + j-1] + 1)
        return p[0][-1]