File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed
Dynamic_Programming/115.Distinct-Subsequences Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -4,20 +4,27 @@ class Solution {
44 {
55 int m = s.size ();
66 int n = t.size ();
7- auto dp = vector<vector<long >>(m+1 ,vector<long >(n+1 ,0 ));
7+ auto dp = vector<vector<long long >>(m+1 ,vector<long long >(n+1 ,0 ));
88 s = " #" +s;
99 t = " #" +t;
10+
11+ if (m<n)
12+ return 0 ;
13+ if (m==n)
14+ return (s==t);
1015
11- for (int i=0 ; i<=m; i++) dp[i][0 ] = 1 ;
16+ for (int i=0 ; i<=m; i++)
17+ dp[i][0 ] = 1 ;
1218
1319 for (int i=1 ; i<=m; i++)
1420 for (int j=1 ; j<=n; j++)
1521 {
1622 if (s[i]==t[j])
17- dp[i][j] += dp[i-1 ][j-1 ];
18- dp[i][j] += dp[i-1 ][j];
23+ dp[i][j] = dp[i-1 ][j] + dp[i-1 ][j-1 ];
24+ else
25+ dp[i][j] = dp[i-1 ][j];
26+ // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
1927 }
20-
2128 return dp[m][n];
2229 }
2330};
You can’t perform that action at this time.
0 commit comments