-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
40 lines (33 loc) · 1.04 KB
/
Copy pathsolution.cpp
File metadata and controls
40 lines (33 loc) · 1.04 KB
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
class Solution
{
public:
int countWays(string &s1, string &s2)
{
// Mod value given in the problem
const int MOD = 1000000007;
int n = s1.size();
int m = s2.size();
// dp[i][j] = ways to form first j characters of s2
// using first i characters of s1
vector<vector<long long>> dp(n + 1, vector<long long>(m + 1, 0));
// Empty string can always be formed in one way
for (int i = 0; i <= n; i++)
dp[i][0] = 1;
// Fill the DP table
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
// Skip current character of s1
dp[i][j] = dp[i - 1][j];
// If characters match, include current character as well
if (s1[i - 1] == s2[j - 1])
{
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD;
}
}
}
// Final answer
return dp[n][m];
}
};