Skip to content

Commit bb7007e

Browse files
committed
Time: 57 ms (63.00%), Space: 6.5 MB (87.07%) - LeetHub
1 parent 57fc1bc commit bb7007e

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
class Solution {
22
public:
33

4-
int helper(int i, int j ,string s, string t, vector<vector<int>>& dp)
5-
{
6-
if(j < 0)
7-
return 1;
8-
if(i < 0)
9-
return 0;
10-
11-
if(dp[i][j] != -1)
12-
return dp[i][j];
13-
14-
if(s[i] == t[j])
15-
return dp[i][j] = helper(i-1,j-1,s,t,dp) + helper(i-1,j,s,t,dp);
16-
return dp[i][j] = helper(i-1,j,s,t,dp);
17-
}
18-
19-
20-
214
int numDistinct(string s, string t) {
22-
5+
236
int n = s.size(), m = t.size();
24-
vector<vector<int>> dp(n+1,vector<int>(m+1,-1));
25-
return helper(n-1,m-1,s,t,dp);
7+
vector<double> prev(m+1,0);
8+
9+
for(int i = 0; i<=n; ++i)
10+
prev[0] = 1;
11+
12+
for(int i= 1; i<=n; ++i)
13+
{
14+
for(int j= m; j>=1; --j)
15+
{
16+
if(s[i-1] == t[j-1])
17+
prev[j] = prev[j-1] + prev[j];
18+
else
19+
prev[j] = prev[j];
20+
}
21+
}
22+
return (int)prev[m];
2623
}
2724
};

0 commit comments

Comments
 (0)