Skip to content

Commit 81cd868

Browse files
authored
Update maximum-deletions-on-a-string.py
1 parent 4d4d6f0 commit 81cd868

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Python/maximum-deletions-on-a-string.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ def deleteString(self, s):
1010
"""
1111
if all(x == s[0] for x in s):
1212
return len(s)
13-
dp = [[0]*(len(s)+1) for i in xrange(2)] # dp[i%2][j]: max prefix length of s[i:] and s[j:]
14-
dp2 = [1]*len(s) # dp2[i]: max operation count of s[i:]
13+
dp2 = [[0]*(len(s)+1) for i in xrange(2)] # dp2[i%2][j]: max prefix length of s[i:] and s[j:]
14+
dp = [1]*len(s) # dp[i]: max operation count of s[i:]
1515
for i in reversed(xrange(len(s)-1)):
1616
for j in xrange(i+1, len(s)):
17-
dp[i%2][j] = dp[(i+1)%2][j+1]+1 if s[j] == s[i] else 0
18-
if dp[i%2][j] >= j-i:
19-
dp2[i] = max(dp2[i], dp2[j]+1)
20-
return dp2[0]
17+
dp2[i%2][j] = dp2[(i+1)%2][j+1]+1 if s[j] == s[i] else 0
18+
if dp2[i%2][j] >= j-i:
19+
dp[i] = max(dp[i], dp[j]+1)
20+
return dp[0]
2121

2222

2323
# Time: O(n^2)
@@ -42,7 +42,7 @@ def getPrefix(pattern, start):
4242

4343
if all(x == s[0] for x in s):
4444
return len(s)
45-
dp = [1]*len(s) # dp[i]: max operations of s[i:]
45+
dp = [1]*len(s) # dp[i]: max operation count of s[i:]
4646
for i in reversed(xrange(len(s)-1)):
4747
prefix = getPrefix(s, i) # prefix[j]+1: longest prefix suffix length of s[i:j+1]
4848
for j in xrange(1, len(prefix), 2):
@@ -73,7 +73,7 @@ def hash(i, j):
7373
for idx, p in enumerate(P):
7474
power[idx].append((power[idx][-1]*p)%MOD)
7575
prefix[idx].append((prefix[idx][-1]*p+(ord(x)-ord('a')))%MOD)
76-
dp = [1]*len(s) # dp[i]: max operations of s[i:]
76+
dp = [1]*len(s) # dp[i]: max operation count of s[i:]
7777
for i in reversed(xrange(len(s)-1)):
7878
for j in xrange(1, (len(s)-i)//2+1):
7979
if hash(i, i+j-1) == hash(i+j, i+2*j-1):

0 commit comments

Comments
 (0)