forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Deletions on a String.cpp
58 lines (46 loc) · 1.26 KB
/
Maximum Deletions on a String.cpp
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
51
52
53
54
55
56
57
58
class Solution {
public:
// kmp
vector<int> prefix_function(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
// dp
int dp[4100];
int helper(string s){
int m = s.size();
// dp memo
if(dp[m]!=-1){
return dp[m];
}
vector<int> prefix = prefix_function(s);
bool check = false;
int ans = 0;
for(int j = 1 ; j < m ; j++){
// checking if prefix[i] == half of length of string s at that particular time
int val = (j+1)/2;
if(j%2==1 and prefix[j] == val){
check = true;
ans = max(ans,1+helper(s.substr(prefix[j])));
}
}
// if no prefix found in string s
if(check==false){
return 1;
}
return dp[m] = ans;
}
int deleteString(string s) {
memset(dp,-1,sizeof(dp));
return helper(s);
}
};