forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindromeIII.java
50 lines (45 loc) · 1.33 KB
/
ValidPalindromeIII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package dynamic_programming;
import java.util.Arrays;
/**
* Created by gouthamvidyapradhan on 23/04/2020 Given a string s and an integer k, find out if the
* given string is a K-Palindrome or not.
*
* <p>A string is K-Palindrome if it can be transformed into a palindrome by removing at most k
* characters from it.
*
* <p>Example 1:
*
* <p>Input: s = "abcdeca", k = 2 Output: true Explanation: Remove 'b' and 'e' characters.
*
* <p>Constraints:
*
* <p>1 <= s.length <= 1000 s has only lowercase English letters. 1 <= k <= s.length
*/
public class ValidPalindromeIII {
public static void main(String[] args) {
System.out.println(new ValidPalindromeIII().isValidPalindrome("abc", 0));
}
int[][] DP;
public boolean isValidPalindrome(String s, int k) {
DP = new int[s.length()][s.length()];
for (int i = 0; i < s.length(); i++) {
Arrays.fill(DP[i], -1);
}
return dp(0, s.length() - 1, s) <= k;
}
private int dp(int i, int j, String S) {
if (i == j) return 0;
else if (i > j) return 0;
else if (DP[i][j] != -1) return DP[i][j];
else {
int min = Integer.MAX_VALUE;
if (S.charAt(i) != S.charAt(j)) {
min = Math.min(min, Math.min(dp(i + 1, j, S), dp(i, j - 1, S)) + 1);
} else {
min = dp(i + 1, j - 1, S);
}
DP[i][j] = min;
return min;
}
}
}