Skip to content

Commit 94c9d86

Browse files
authored
Create longest-string-chain.py
1 parent 7e25337 commit 94c9d86

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/longest-string-chain.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n * l^2)
2+
# Space: O(n * l)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def longestStrChain(self, words):
9+
"""
10+
:type words: List[str]
11+
:rtype: int
12+
"""
13+
words.sort(key=len)
14+
dp = collections.defaultdict(int)
15+
for w in words:
16+
for i in xrange(len(w)):
17+
dp[w] = max(dp[w], dp[w[:i]+w[i+1:]]+1)
18+
return max(dp.itervalues())

0 commit comments

Comments
 (0)