返回介绍

solution / 1200-1299 / 1216.Valid Palindrome III / README_EN

发布于 2024-06-17 01:03:21 字数 5801 浏览 0 评论 0 收藏 0

1216. Valid Palindrome III

中文文档

Description

Given a string s and an integer k, return true if s is a k-palindrome.

A string is k-palindrome if it can be transformed into a palindrome by removing at most k characters from it.

 

Example 1:

Input: s = "abcdeca", k = 2
Output: true
Explanation: Remove 'b' and 'e' characters.

Example 2:

Input: s = "abbababa", k = 1
Output: true

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.
  • 1 <= k <= s.length

Solutions

Solution 1: Dynamic Programming

The problem requires us to remove at most $k$ characters to make the remaining string a palindrome. This can be transformed into finding the longest palindromic subsequence.

We define $f[i][j]$ as the length of the longest palindromic subsequence in the substring $s[i..j]$. Initially, we have $f[i][i] = 1$ for all $i$, since each single character is a palindrome.

If $s[i] = s[j]$, then we have $f[i][j] = f[i+1][j-1] + 2$, since we can add both $s[i]$ and $s[j]$ to the longest palindromic subsequence of $s[i+1..j-1]$.

If $s[i] \neq s[j]$, then we have $f[i][j] = \max(f[i+1][j], f[i][j-1])$, since we need to remove either $s[i]$ or $s[j]$ to make the remaining substring a palindrome.

Finally, we check whether there exists $f[i][j] + k \geq n$, where $n$ is the length of the string $s$. If so, it means that we can remove at most $k$ characters to make the remaining string a palindrome.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the string $s$.

class Solution:
  def isValidPalindrome(self, s: str, k: int) -> bool:
    n = len(s)
    f = [[0] * n for _ in range(n)]
    for i in range(n):
      f[i][i] = 1
    for i in range(n - 2, -1, -1):
      for j in range(i + 1, n):
        if s[i] == s[j]:
          f[i][j] = f[i + 1][j - 1] + 2
        else:
          f[i][j] = max(f[i + 1][j], f[i][j - 1])
        if f[i][j] + k >= n:
          return True
    return False
class Solution {
  public boolean isValidPalindrome(String s, int k) {
    int n = s.length();
    int[][] f = new int[n][n];
    for (int i = 0; i < n; ++i) {
      f[i][i] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
      for (int j = i + 1; j < n; ++j) {
        if (s.charAt(i) == s.charAt(j)) {
          f[i][j] = f[i + 1][j - 1] + 2;
        } else {
          f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
        }
        if (f[i][j] + k >= n) {
          return true;
        }
      }
    }
    return false;
  }
}
class Solution {
public:
  bool isValidPalindrome(string s, int k) {
    int n = s.length();
    int f[n][n];
    memset(f, 0, sizeof f);
    for (int i = 0; i < n; ++i) {
      f[i][i] = 1;
    }
    for (int i = n - 2; i >= 0; --i) {
      for (int j = i + 1; j < n; ++j) {
        if (s[i] == s[j]) {
          f[i][j] = f[i + 1][j - 1] + 2;
        } else {
          f[i][j] = max(f[i + 1][j], f[i][j - 1]);
        }
        if (f[i][j] + k >= n) {
          return true;
        }
      }
    }
    return false;
  }
};
func isValidPalindrome(s string, k int) bool {
  n := len(s)
  f := make([][]int, n)
  for i := range f {
    f[i] = make([]int, n)
    f[i][i] = 1
  }
  for i := n - 2; i >= 0; i-- {
    for j := i + 1; j < n; j++ {
      if s[i] == s[j] {
        f[i][j] = f[i+1][j-1] + 2
      } else {
        f[i][j] = max(f[i+1][j], f[i][j-1])
      }
      if f[i][j]+k >= n {
        return true
      }
    }
  }
  return false
}
function isValidPalindrome(s: string, k: number): boolean {
  const n = s.length;
  const f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => 0));
  for (let i = 0; i < n; ++i) {
    f[i][i] = 1;
  }
  for (let i = n - 2; ~i; --i) {
    for (let j = i + 1; j < n; ++j) {
      if (s[i] === s[j]) {
        f[i][j] = f[i + 1][j - 1] + 2;
      } else {
        f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
      }
      if (f[i][j] + k >= n) {
        return true;
      }
    }
  }
  return false;
}
impl Solution {
  pub fn is_valid_palindrome(s: String, k: i32) -> bool {
    let s = s.as_bytes();
    let n = s.len();
    let mut f = vec![vec![0; n]; n];

    for i in 0..n {
      f[i][i] = 1;
    }

    for i in (0..n - 2).rev() {
      for j in i + 1..n {
        if s[i] == s[j] {
          f[i][j] = f[i + 1][j - 1] + 2;
        } else {
          f[i][j] = std::cmp::max(f[i + 1][j], f[i][j - 1]);
        }

        if f[i][j] + k >= (n as i32) {
          return true;
        }
      }
    }

    false
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文