-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathString compression 2.cpp
More file actions
31 lines (29 loc) · 942 Bytes
/
String compression 2.cpp
File metadata and controls
31 lines (29 loc) · 942 Bytes
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
/* Leetcode 1531 question link - https://leetcode.com/problems/string-compression-ii/
Applied Dynamic Programming conecpt in this question - Memoization */
// code-
int dp[101][27][101][101];
class Solution {
int f(int ind,string &s,int prev,int len,int k){
if(k<0) return INT_MAX;
if(ind>=s.size()) return 0;
if(dp[ind][prev][len][k]!=-1) return dp[ind][prev][len][k];
int not_pick=f(ind+1,s,prev,len,k-1);
int pick=0;
if(s[ind]-'a'== prev){
if(len==1 || len==9 || len==99){
pick+=1;
}
pick=pick+f(ind+1,s,prev,len+1,k);
}
else{
pick=1+f(ind+1,s,s[ind]-'a',1,k);
}
return dp[ind][prev][len][k]=min(pick,not_pick);
}
public:
int getLengthOfOptimalCompression(string s, int k) {
int n=s.length();
memset(dp,-1,sizeof(dp));
return f(0,s,26,0,k);
}
};