Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5099. 验证回文字符串 III #22

Open
3 tasks
JalanJiang opened this issue Oct 5, 2019 · 1 comment
Open
3 tasks

5099. 验证回文字符串 III #22

JalanJiang opened this issue Oct 5, 2019 · 1 comment
Assignees
Labels

Comments

@JalanJiang
Copy link
Owner


  • 输出题解
  • 时间复杂度
  • 是否最优解
@JalanJiang
Copy link
Owner Author

  1. 反转字符串 s 记为 t
  2. dp[i][j] 表示 s[i]t[j] 拥有相同字母数
class Solution:
    def isValidPalindrome(self, s: str, k: int) -> bool:
        t = s[::-1]
        length = len(s)
        dp = [[0 for _ in range(length + 1)] for _ in range(length + 1)]
        for i in range(1, length + 1):
            for j in range(1, length + 1):
                if s[i - 1] == t[j - 1]:
                    dp[i][j] = dp[i - 1][j - 1] + 1
                else:
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
        
        return length - dp[length][length] <= k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants