Longest String Chain LeetCode solution in Python

1
# Runtime: 123 ms (Top 85.4%) | Memory: 16.81 MB (Top 58.4%)
2

3

4
class Solution:
5
def longestStrChain(self, words: List[str]) -> int:
6

7
words.sort(key=len)
8
dic = {}
9

10
for i in words:
11
dic[i] = 1
12

13
for j in range(len(i)):
14

15
# creating words by deleting a letter
16
successor = i[:j] + i[j + 1 :]
17
if successor in dic:
18
dic[i] = max(dic[i], 1 + dic[successor])
19

20
res = max(dic.values())
21
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0