-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1048.Longest-String-Chain.py
60 lines (49 loc) · 1.56 KB
/
1048.Longest-String-Chain.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# https://leetcode.com/problems/longest-string-chain/
# Easy (44.06%)
# Total Accepted: 3,426
# Total Submissions: 7,775
# beats 100.0% of python submissions
class Solution(object):
def longestStrChain(self, words):
"""
:type words: List[str]
:rtype: int
"""
length = len(words)
if length < 2:
return 1
w_len_map = {}
w_map = {}
max_w_len = float('-inf')
for w in words:
wordLen = len(w)
max_w_len = max(max_w_len, wordLen)
if wordLen in w_len_map:
w_len_map[wordLen] += w,
else:
w_len_map[wordLen] = [w]
w_map[w] = True
res = 1
q = {}
for i in xrange(max_w_len, 0, -1):
if i not in w_len_map:
q = {}
continue
if not q:
q = {w:1 for w in w_len_map[i]}
continue
tmp = {w:1 for w in w_len_map[i]}
for k, v in q.iteritems():
res = max(res, v)
is_found = False
for j in xrange(i + 1):
next_w = k[:j] + k[j+1:]
if next_w in tmp:
is_found = True
tmp[next_w] = v + 1
if not is_found:
res = max(res, v)
else:
res = max(res, v + 1)
q = tmp
return res