@@ -10,14 +10,14 @@ def deleteString(self, s):
10
10
"""
11
11
if all (x == s [0 ] for x in s ):
12
12
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:]
15
15
for i in reversed (xrange (len (s )- 1 )):
16
16
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 ]
21
21
22
22
23
23
# Time: O(n^2)
@@ -42,7 +42,7 @@ def getPrefix(pattern, start):
42
42
43
43
if all (x == s [0 ] for x in s ):
44
44
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:]
46
46
for i in reversed (xrange (len (s )- 1 )):
47
47
prefix = getPrefix (s , i ) # prefix[j]+1: longest prefix suffix length of s[i:j+1]
48
48
for j in xrange (1 , len (prefix ), 2 ):
@@ -73,7 +73,7 @@ def hash(i, j):
73
73
for idx , p in enumerate (P ):
74
74
power [idx ].append ((power [idx ][- 1 ]* p )% MOD )
75
75
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:]
77
77
for i in reversed (xrange (len (s )- 1 )):
78
78
for j in xrange (1 , (len (s )- i )// 2 + 1 ):
79
79
if hash (i , i + j - 1 ) == hash (i + j , i + 2 * j - 1 ):
0 commit comments