-
Notifications
You must be signed in to change notification settings - Fork 40
/
26 August "Problem of the Day" Answer
45 lines (31 loc) · 1.15 KB
/
26 August "Problem of the Day" Answer
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
GeeksforGeeks
The Question is :- "Count Palindromic Subsequences" the solution is :-
Answer :-
class Solution{
data;
public:
/*You are required to complete below method */
long long int countPS(string str)
{ int n=str.size();
long long int m=1000000007;
vector<vector<long long int>> dp(n,vector<long long int>(n,0));
for(int i=0;i<n;i++){
dp[i][i]=1;
}
for(int i=1;i<n;i++){
for(int j=0;j+i<n;j++){
if(str[j]==str[j+i]){
dp[j][j+i]=(1+(dp[j+1][j+i])+(dp[j][j+i-1]))%m;
}
else{
dp[j][j+i]=(((dp[j+1][j+i])+(dp[j][j+i-1]))-(dp[j+1][j+i-1]) +m)%m;
}
}
}
return dp[0][n-1];
//Your code here
}
};
Hope you understand the answer, and complete it.
Stay Connected for daily Problem of the Day answers.
Thank you All!