forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode-string-with-shortest-length.py
More file actions
29 lines (26 loc) · 1017 Bytes
/
encode-string-with-shortest-length.py
File metadata and controls
29 lines (26 loc) · 1017 Bytes
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
# Time: O(n^3) on average
# Space: O(n^2)
class Solution(object):
def encode(self, s):
"""
:type s: str
:rtype: str
"""
def encode_substr(dp, s, i, j):
temp = s[i:j+1]
pos = (temp + temp).find(temp, 1) # O(n) on average
if pos >= len(temp):
return temp
return str(len(temp)/pos) + '[' + dp[i][i + pos - 1] + ']'
dp = [["" for _ in xrange(len(s))] for _ in xrange(len(s))]
for length in xrange(1, len(s)+1):
for i in xrange(len(s)+1-length):
j = i+length-1
dp[i][j] = s[i:i+length]
for k in xrange(i, j):
if len(dp[i][k]) + len(dp[k+1][j]) < len(dp[i][j]):
dp[i][j] = dp[i][k] + dp[k+1][j]
encoded_string = encode_substr(dp, s, i, j)
if len(encoded_string) < len(dp[i][j]):
dp[i][j] = encoded_string
return dp[0][len(s) - 1]