1682. Longest Palindromic Subsequence II

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1682

class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        @cache
        def dfs(i, j, x):
            if i >= j:
                return 0
            if s[i] == s[j] and s[i] != x:
                return dfs(i + 1, j - 1, s[i]) + 2
            return max(dfs(i + 1, j, x), dfs(i, j - 1, x))

        res = dfs(0, len(s) - 1, '')
        dfs.cache_clear()
        return res