-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dp_n*m.py
34 lines (29 loc) · 837 Bytes
/
AC_dp_n*m.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_dp_n*m.py
# Create Date: 2015-03-11 11:35:56
# Usage: AC_dp_n*m.py
# Descripton:
class Solution:
# @return an integer
def numDistinct(self, S, T):
n = len(S)
m = len(T)
dp = [[0] * (m + 1), [0] * (m + 1)]
dp[0][0] = 1
dp[1][0] = 1
cur = 0
for i in xrange(1, n + 1):
cur = 1 - cur
for j in xrange(1, m + 1):
if S[i - 1] == T[j - 1]:
dp[cur][j] = dp[1-cur][j] + dp[1-cur][j-1]
else:
dp[cur][j] = dp[1-cur][j]
return dp[cur][m]
# debug
s = Solution()
print s.numDistinct('ccc', 'c')
print s.numDistinct('', 'a')
print s.numDistinct('rabbbit', 'rabbit')