647. Palindromic Substrings 516. Longest Palindromic Subsequence

647. Palindromic Substrings

Given a string s, return the number of palindromic substrings 回文子串 in it.

A string is a palindrome when it reads the same backward as forward.

substring is a contiguous sequence of characters within the string.

nomal:

Time complexity: O(n^2)
Space complexity: O(1)

class Solution:
    def countSubstrings(self, s: str) -> int:
        count = 0

        for left in range(len(s)):
            for right in range(left, len(s)):
                if s[left : right + 1] == s[left : right + 1][::-1]: # wrong:[-1]最后一个字符
                    count += 1
        return count

DP:

Time complexity: O(n^2)
Space complexity: O(n^2)

class Solution:
    def countSubstrings(self, s: str) -> int:
        dp = [[False] * len(s) for _ in range(len(s))]
        result = 0
        for i in range(len(s)-1, -1, -1): #注意遍历顺序
            for j in range(i, len(s)):
                if s[i] == s[j]:
                    if j - i <= 1: #情况一 和 情况二
                        result += 1
                        dp[i][j] = True
                    elif dp[i+1][j-1]: #情况三
                        result += 1
                        dp[i][j] = True
        return result

 2 pointer:

Determining the palindrome string is a matter of finding the center and then spreading it out to both sides to see if it is symmetrical.

When traversing the central point, it is important to note that there are two cases of central points.

One element can be used as the center point, and two elements can be used as the center point.

Time complexity: O(n^2)
Space complexity: O(1)

class Solution:
    def countSubstrings(self, s: str) -> int:
        result = 0
        for i in range(len(s)):
            result += self.extend(s, i, i, len(s)) #以i为中心
            result += self.extend(s, i, i+1, len(s)) #以i和i+1为中心
        return result
    
    def extend(self, s, i, j, n):
        res = 0
        while i >= 0 and j < n and s[i] == s[j]:
            i -= 1
            j += 1
            res += 1
        return res

516. Longest Palindromic Subsequence

Given a string s, find the longest palindromic subsequence回文子序列's length in s.

subsequence is a sequence that can be derived衍生 from another sequence by deleting some or no elements without changing the order of the remaining elements.

1. dp[i][j]: the length of the longest palindrome subsequence of the string s in the range [i, j] is                        dp[i][j].

2. If s[i] is the same as s[j]: then dp[i][j] = dp[i + 1][j - 1] + 2;

3. If s[i] is not the same as s[j]: then dp[i][j] must be taken to be maximal,

                        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​     dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);

 4. Determine the traversal order

5. initial 

    for i in range(len(s)):

              dp[i][i] == 1

6. return

DP: 

Time complexity: O(n^2)
Space complexity: O(n^2)

class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        dp = [[0] * len(s) for _ in range(len(s))]
        for i in range(len(s)):
            dp[i][i] = 1
        for i in range(len(s)-1, -1, -1):
            for j in range(i+1, len(s)):
                if s[i] == s[j]:
                    dp[i][j] = dp[i+1][j-1] + 2
                else:
                    dp[i][j] = max(dp[i+1][j], dp[i][j-1])
        return dp[0][-1]

dfs:

class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        n = len(s)

        # 使用记忆化搜索实现
        @cache
        def dfs(i, j):
            if i > j:
                return 0
            if i == j:
                return 1
            if s[i] == s[j]:
                return dfs(i + 1, j - 1) + 2
            else:
                return max(dfs(i + 1, j), dfs(i, j - 1))
        return dfs(0, n - 1)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 11:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 11:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 11:18:01       82 阅读
  4. Python语言-面向对象

    2023-12-06 11:18:01       91 阅读

热门阅读

  1. LightDB - 支持 last_day 函数[mysql兼容]

    2023-12-06 11:18:01       56 阅读
  2. NLP中几个简单的,字符串相似度计算方法

    2023-12-06 11:18:01       53 阅读
  3. AI:大语言模型LLM

    2023-12-06 11:18:01       60 阅读
  4. Pytest 的小例子

    2023-12-06 11:18:01       57 阅读
  5. css基础

    2023-12-06 11:18:01       58 阅读
  6. 什么是供应链金融分账系统?

    2023-12-06 11:18:01       59 阅读
  7. vue2和vue3的区别

    2023-12-06 11:18:01       55 阅读
  8. oracle给用户授权查询权限

    2023-12-06 11:18:01       60 阅读
  9. MISRA C++ 2008 标准解析

    2023-12-06 11:18:01       50 阅读