mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -145,7 +145,28 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int numDistinct(String s, String t) {
|
||||||
|
int[][] dp = new int[s.length() + 1][t.length() + 1];
|
||||||
|
for (int i = 0; i < s.length() + 1; i++) {
|
||||||
|
dp[i][0] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < s.length() + 1; i++) {
|
||||||
|
for (int j = 1; j < t.length() + 1; j++) {
|
||||||
|
if (s.charAt(i - 1) == t.charAt(j - 1)) {
|
||||||
|
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
|
||||||
|
}else{
|
||||||
|
dp[i][j] = dp[i - 1][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[s.length()][t.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
Reference in New Issue
Block a user