Skip to content

Commit 468960a

Browse files
committed
Time: 204 ms (5.09%), Space: 370.2 MB (5.03%) - LeetHub
1 parent b6f42b8 commit 468960a

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
class Solution {
22
public:
3-
#define mod 1e9+7;
4-
int numDistinct(string s, string t) {
5-
int n=s.size(), m=t.size();
3+
int helper(string &s, string &t, int n, int m, vector<vector<int>> &dp){
4+
if(n==0 && m==0)
5+
return 1;
6+
7+
if(n==0)
8+
return 0;
69

7-
vector<vector<int>> dp(n+1, vector<int>(m+1,0));
10+
if(m==0)
11+
return 1;
812

9-
for(int i=0; i<=n; i++){
10-
for(int j=0; j<=m; j++){
11-
dp[0][j]=0;
12-
13-
dp[i][0]=1;
14-
}
15-
}
13+
if(dp[n][m]!=-1)
14+
return dp[n][m];
1615

17-
dp[0][0]=1;
16+
if(s[n-1]==t[m-1])
17+
return dp[n][m]=helper(s, t, n-1, m-1, dp) + helper(s, t, n-1, m, dp);
1818

19-
for(int i=1; i<=n; i++){
20-
for(int j=1; j<=m; j++){
21-
22-
if(s[i-1]==t[j-1])
23-
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%1000000007;
24-
25-
else dp[i][j]=max(dp[i][j], dp[i-1][j]);
26-
}
27-
}
19+
else return dp[n][m]=helper(s, t, n-1, m, dp);
20+
21+
}
22+
23+
int numDistinct(string s, string t) {
24+
int n=s.size(), m=t.size();
25+
vector<vector<int>> dp(1001, vector<int> (1001, -1));
2826

29-
return dp[n][m];
27+
return helper(s, t, n, m, dp);
3028
}
3129
};

0 commit comments

Comments
 (0)