Skip to content

Commit

Permalink
Create 2478.Number-of-Beautiful-Partitions_v2.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Nov 24, 2022
1 parent e1a4fc2 commit 14418f2
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using LL = long long;
class Solution {
LL dp[1005][1005];
LL M = 1e9+7;
public:
int beautifulPartitions(string s, int K, int minLength)
{
int n = s.size();
s = "#"+s;

dp[0][0] = 1;
for (int j=1; j<=K; j++)
{
LL sum = 0;
for (int i=1; i<=n; i++)
{
if (i-minLength>=0 && !isprime(s[i-minLength]) && isprime(s[i-minLength+1]))
{
sum += dp[i-minLength][j-1];
sum %= M;
}
if (!isprime(s[i])) {
dp[i][j] = sum;
}
}
}
return dp[n][K];
}

bool isprime(char ch)
{
return ch == '2' || ch == '3' || ch == '5' || ch == '7';
}
};

0 comments on commit 14418f2

Please sign in to comment.