description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
def extraChar(s1, s2):
l1, l2 = sorted(s1), sorted(s2)
diff = 0
i, j = 0, 0
if len(s1) == len(s2) + 1:
while i < len(s1) and j < len(s2):
if l1[i] != l2[j]:
i += 1
diff += 1
else:
i += 1
j += 1
if i == len(s1) and j == len(s2):
return diff == 1
elif i == len(s1) - 1:
return True
return False
mem = {}
words.sort(key=lambda x: len(x))
def dp(i):
if i >= n:
return 0
if i in mem:
return mem[i]
mem[i] = 1
for j in range(i + 1, n):
if len(words[j]) == len(words[i]) + 1 and extraChar(words[j], words[i]):
mem[i] = max(mem[i], dp(j) + 1)
elif len(words[j]) > len(words[i]) + 1:
break
return mem[i]
return max([dp(i) for i in range(n)])
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
result = 1
for word in sorted(words, key=len):
dp[word] = 1
for i in range(len(word)):
prev = word[:i] + word[i + 1 :]
if prev in dp:
dp[word] = max(dp[word], dp[prev] + 1)
result = max(result, dp[word])
return result
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def isPred(w1, w2):
i, j = 0, 0
while i < len(w1):
if w1[i] != w2[j]:
if j != i:
return False
j += 1
else:
j += 1
i += 1
return True
words = sorted(words, key=lambda x: len(x))
dp = [1] * len(words)
for i in range(1, len(words)):
for j in range(i):
if len(words[i]) == len(words[j]) + 1 and isPred(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
di = defaultdict(list)
for w in words:
di[len(w)].append(w)
L, U = min(di.keys()), max(di.keys())
dp = [1] * len(di[L])
ans = 1
def ispred(w1, w2):
skip = False
for i in range(len(w1)):
while w1[i] != w2[i + skip]:
if skip:
return False
skip = True
return True
for l in range(L + 1, U + 1):
old = dp
dp = [1] * len(di[l])
for j, w2 in enumerate(di[l]):
tmp = [old[i] for i, w1 in enumerate(di[l - 1]) if ispred(w1, w2)]
if tmp:
dp[j] = max(tmp) + 1
ans = max(ans, max(dp))
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
words_dict = {}
for word in words:
dp[word] = -1
if words_dict.get(len(word)):
words_dict[len(word)].append(word)
else:
words_dict[len(word)] = [word]
result = 0
for word in words:
result = max(result, self.longest_chain(word, dp, words_dict))
return result
def longest_chain(self, word, dp, words_dict):
if dp[word] != -1:
return dp[word]
if not words_dict.get(len(word) + 1):
return 1
res = 0
for word2 in words_dict[len(word) + 1]:
if self.isSucc(word, word2):
res = max(res, self.longest_chain(word2, dp, words_dict))
res += 1
return res
def isSucc(self, word1, word2):
if len(word1) != len(word2) - 1:
return False
count = 0
i = 0
j = 0
while i < len(word1):
if word1[i] != word2[j]:
if count == 1:
return False
count += 1
j += 1
else:
j += 1
i += 1
return True
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
dp = [(1) for _ in words]
for start in range(len(words)):
for end in range(start - 1, -1, -1):
if len(words[end]) < len(words[start]) - 1:
break
if predecessor(words[start], words[end]):
dp[start] = max(dp[start], 1 + dp[end])
return max(dp)
def predecessor(word, candidate):
if len(word) != len(candidate) + 1:
return False
j = 0
for i, c in enumerate(word):
if i > j + 1 or j == len(candidate):
break
if c == candidate[j]:
j += 1
return j == len(candidate)
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def checkPredecessor(self, word_1, word_2):
find_extra = False
for i in range(len(word_1)):
chr_1 = word_1[i]
if not find_extra:
chr_2 = word_2[i]
else:
chr_2 = word_2[i + 1]
if chr_1 != chr_2:
if find_extra:
return False
else:
if chr_1 != word_2[i + 1]:
return False
find_extra = True
return True
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
longest_seq = [1] * len(words)
for i in range(1, len(words)):
curr_word = words[i]
for j in range(0, i)[::-1]:
prev_word = words[j]
if len(prev_word) == len(curr_word):
continue
if len(prev_word) + 1 < len(curr_word):
break
if self.checkPredecessor(prev_word, curr_word):
longest_seq[i] = max(longest_seq[i], longest_seq[j] + 1)
return max(longest_seq)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def check(w1, w2):
if len(w1) != len(w2) - 1:
return False
cnt = 0
idx1 = 0
idx2 = 0
while idx1 < len(w1) and idx2 < len(w2) and cnt <= 1:
if w1[idx1] == w2[idx2]:
idx1 += 1
idx2 += 1
else:
cnt += 1
idx2 += 1
if cnt == 0 and idx1 == len(w1) and idx2 == len(w2) - 1:
return True
elif cnt == 1 and idx1 == len(w1) and idx2 == len(w2):
return True
else:
return False
words.sort(key=len)
dp = [(1) for _ in range(len(words))]
for j in range(len(words)):
for i in range(j):
if check(words[i], words[j]):
dp[j] = max(dp[j], dp[i] + 1)
res = max(dp)
return res
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def isSubString(self, s1, s2):
diff = 0
l = len(s1)
ll = len(s2)
if ll - l != 1:
return False
i = 0
j = 0
diff = 0
while i < l and diff < 2:
if s1[i] == s2[j]:
i += 1
j += 1
else:
j += 1
diff += 1
if diff > 1:
return False
return True
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
words = sorted(words, key=len)
dp = [1] * n
for i in range(n):
j = i - 1
while j >= 0 and len(words[i]) - len(words[j]) <= 1:
if self.isSubString(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
j -= 1
return max(dp)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
graph = {}
max_dist = 0
for word in words:
length = len(word)
dist = 0
for i in range(length):
if (check := word[:i] + word[i + 1 :]) in graph:
dist = max(graph[check], dist)
graph[word] = dist + 1
max_dist = max(max_dist, graph[word])
return max_dist
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def check(i, j):
w1 = words[i]
w2 = words[j]
for k in range(len(w2)):
if w1 == w2[:k] + w2[k + 1 :]:
return True
return False
buckets = defaultdict(list)
for i, word in enumerate(words):
l = len(word)
buckets[l] += [i]
g = defaultdict(list)
for key in buckets:
if key + 1 in buckets:
for i in buckets[key]:
for j in buckets[key + 1]:
if check(i, j):
g[i] += [j]
ans = 0
seen = set()
for key in sorted(buckets.keys()):
for i in buckets[key]:
stack = [[1, i]]
while stack:
level, node = stack.pop()
ans = max(ans, level)
if node in seen:
continue
seen.add(node)
if node in g:
for child in g[node]:
if child not in seen:
stack += [[level + 1, child]]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def can_move(self, a, b):
b_letters = Counter(b)
for char in a:
b_letters[char] -= 1
if b_letters[char] < 0:
return False
elif b_letters[char] == 0:
b_letters.pop(char)
return b_letters.popitem()[1] == 1
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda w: len(w))
depths = {}
def recur(index):
if index not in depths:
s = words[index]
mx = 0
for i in range(index + 1, len(words)):
if len(words[i]) - len(s) == 1 and self.can_move(s, words[i]):
mx = max(recur(i), mx)
depths[index] = mx + 1
return depths[index]
for i in range(len(words)):
recur(i)
return max(depths.values())
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def predecessor(self, w1, w2):
i = 0
diffs = 0
while i < len(w1):
if w1[i] != w2[i]:
diffs = 1
break
i += 1
if diffs == 0:
return True
return w1 == w2[:i] + w2[i + 1 :]
def longestStrChain(self, words):
words = sorted(words, key=lambda x: len(x))
words_by_len = {}
for w in words:
if len(w) in words_by_len:
words_by_len[len(w)].append(w)
else:
words_by_len[len(w)] = [w]
max_lens = {}
for w in words:
max_lens[w] = 1
for l in words_by_len:
if l - 1 in words_by_len:
for w1 in words_by_len[l]:
for w2 in words_by_len[l - 1]:
if self.predecessor(w2, w1):
max_lens[w1] = max(max_lens[w1], 1 + max_lens[w2])
max_len = 0
for w in max_lens:
max_len = max(max_len, max_lens[w])
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
word_list = sorted(words, key=lambda x: len(x))
word_dict = {word: (1) for word in word_list}
longest = 0
for word in word_list:
for i in range(len(word)):
test = word[:i] + word[i + 1 :]
if test in word_dict:
word_dict[word] = word_dict[test] + 1
longest = max(longest, word_dict[word])
return longest
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = collections.defaultdict(int)
for w in sorted(words, key=len):
for i in range(len(w)):
dp[w] = max(dp[w], 1 + dp[w[:i] + w[i + 1 :]])
return max(dp.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words = sorted(words, key=len)
map = {}
DP = [(1) for idx in range(len(words))]
for i in range(len(words)):
map = {}
for j in range(0, i):
if len(words[i]) == len(words[j]) + 1:
diff = 0
for comp in range(len(words[i])):
if diff == 0 and comp == len(words[i]) - 1:
diff = 1
continue
if words[i][comp] == words[j][comp - diff]:
continue
elif diff == 1:
diff = 69
break
else:
diff = 1
if diff == 1:
if DP[j] + 1 > DP[i]:
DP[i] = DP[j] + 1
return max(DP)
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if len(words) == 0:
return False
words.sort(key=lambda x: len(x))
def check(a, b):
for i in range(len(b) - 1):
if a[i] != b[i]:
return b[i + 1 :] == a[i:]
return True
ans = 1
dp = [1] * len(words)
for i in range(len(words)):
for j in range(i + 1, len(words)):
if len(words[j]) > 1 + len(words[i]):
break
if len(words[j]) == 1 + len(words[i]):
if check(words[i], words[j]):
dp[j] = max(dp[j], 1 + dp[i])
ans = max(ans, dp[j])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words = sorted(words, key=len)
n = len(words)
def match(l, r):
a = words[l]
b = words[r]
if len(a) + 1 != len(b):
return False
i, j = 0, 0
while i < len(a) and j < len(b):
if a[i] == b[j]:
i += 1
j += 1
return i == len(a)
dp = [(0) for _ in range(n)]
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
if len(words[j]) > len(words[i]) + 1:
break
if match(i, j):
dp[i] = max(dp[i], 1 + dp[j])
return 1 + max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
words.sort(key=len)
f = [1] * n
def ispre(w1, w2):
if len(w1) != len(w2) - 1:
return False
it = iter(w2)
return all(c in it for c in w1)
res = 1
for i in range(1, n):
for j in range(i):
if ispre(words[j], words[i]):
f[i] = max(f[i], f[j] + 1)
res = max(res, f[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
d = collections.defaultdict(list)
for word in words:
d[len(word)].append(word)
lengths = sorted(list(d.keys()))
self.mem = {}
def dfs(word):
if len(word) == lengths[-1]:
return 1
if word in self.mem:
return self.mem[word]
nxt_length = 0
for nxt_word in d[len(word) + 1]:
for j in range(len(nxt_word)):
if word == nxt_word[:j] + nxt_word[j + 1 :]:
nxt_length = max(nxt_length, dfs(nxt_word))
self.mem[word] = nxt_length + 1
return nxt_length + 1
res = 0
for word in words:
res = max(res, dfs(word))
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
size = 0
wordSet = set(words)
mem = {}
def dfs(s):
if s in mem:
return mem[s]
maxpath = 1
for i in range(len(s)):
new = s[:i] + s[i + 1 :]
if new in wordSet:
maxpath = max(maxpath, 1 + dfs(new))
mem[s] = maxpath
return mem[s]
for word in words:
if word not in mem:
size = max(size, dfs(word))
print(mem)
return size
dic = collections.defaultdict(int)
words = sorted(words, key=lambda x: len(x))
for word in words:
dic[word] = 1
for i in range(len(word)):
tmp = word[:i] + word[i + 1 :]
if tmp in dic:
dic[word] = max(dic[word], dic[tmp] + 1)
return max(dic.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
wset = set(words)
memo = {}
def dp(word):
if word in memo:
return memo[word]
ans = 1
for i in range(len(word) + 1):
for j in range(26):
newword = word[:i] + chr(ord("a") + j) + word[i:]
if newword in wset:
ans = max(ans, 1 + dp(newword))
memo[word] = ans
return ans
ret = 0
for word in words:
if not word in memo:
ret = max(ret, dp(word))
return ret
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
l = [(1) for i in range(len(words))]
words.sort(key=lambda x: len(x))
res = [[i] for i in words]
for i in range(1, len(words)):
for j in range(i):
if len(words[j]) + 1 == len(words[i]):
x = {}
for a in words[j]:
if a in x:
x[a] += 1
else:
x[a] = 1
ctr = 0
for a in words[i]:
if a not in x:
ctr += 1
elif x[a] == 0:
ctr += 1
else:
x[a] -= 1
if ctr == 1 and l[i] < l[j] + 1:
res[i] = [i for i in res[j]]
res[i].append([words[i]])
l[i] = l[j] + 1
return max(l)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
def is_pre(s1, s2):
c = 0
i = 0
for j in range(len(s2)):
if i == len(s1) or s1[i] != s2[j]:
c += 1
else:
i += 1
return c == 1
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
n = len(words)
m = {}
incount = {}
q = collections.deque()
seen = set()
for i in range(n):
if i not in incount:
q.append(i)
wn = len(words[i])
for j in range(i + 1, n):
jn = len(words[j])
if jn > wn + 1:
break
if jn == wn + 1 and is_pre(words[i], words[j]):
m.setdefault(i, []).append(j)
incount[j] = incount.get(j, 0) + 1
lev = 0
while q:
lev += 1
for i in range(len(q)):
u = q.popleft()
for v in m.get(u, []):
if v not in seen:
q.append(v)
seen.add(v)
return lev
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR LIST VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
words.sort(key=len)
f = [1] * n
def ispre(w1, w2):
if len(w1) != len(w2) - 1:
return False
i = j = 0
diff = False
while i < len(w1) and j < len(w2):
if w1[i] != w2[j]:
if not diff:
diff = True
j += 1
continue
else:
return False
i += 1
j += 1
return True
res = 1
for i in range(1, n):
for j in range(i):
if ispre(words[j], words[i]):
f[i] = max(f[i], f[j] + 1)
res = max(res, f[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x), reverse=True)
minLen = len(words[-1])
dic = set(words)
seen = set()
self.result = 0
for word in words:
if word not in seen:
length = 1
self.dfs(word, dic, seen, minLen, length)
return self.result
def dfs(self, word, dic, seen, minLen, length):
self.result = max(self.result, length)
if len(word) == minLen:
return
seen.add(word)
for i in range(len(word)):
new = word[:i] + word[i + 1 :]
if new in dic:
self.dfs(new, dic, seen, minLen, length + 1)
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words = set(words)
@lru_cache()
def helper(word):
if not word or word not in words:
return 0
return 1 + max(helper(word[:i] + word[i + 1 :]) for i in range(len(word)))
return max(helper(word) for word in words)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words = sorted(words, key=lambda x: len(x))
dp = [1] * len(words)
for i in range(1, len(words)):
for j in range(i - 1, -1, -1):
if len(words[i]) == len(words[j]) + 1:
update = True
for c in words[j]:
if c not in words[i]:
update = False
if update:
dp[i] = max(dp[i], 1 + dp[j])
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
def isPredecessor(src, dst):
nsrc = len(src)
ndst = len(dst)
if nsrc + 1 != ndst:
return False
i = 0
while i < nsrc:
if src[i] == dst[i]:
i += 1
continue
else:
return src[i:] == dst[i + 1 :]
return True
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
mem_len = [(-1) for _ in range(len(words))]
def search(i):
if mem_len[i] != -1:
return
ans = 1
for j in range(i + 1, len(words)):
if isPredecessor(words[i], words[j]):
search(j)
ans = max(ans, mem_len[j] + 1)
mem_len[i] = ans
for i in range(len(words)):
search(i)
return max(mem_len)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
print(words)
def stringmatch(s1, s2):
onemismatchallowed = True
if len(s1) > len(s2):
s2, s1 = s1, s2
if len(s1) + 1 != len(s2):
return False
i, j = 0, 0
while i < len(s1):
if s1[i] != s2[j]:
if onemismatchallowed == True:
onemismatchallowed = False
else:
return False
j += 1
else:
i += 1
j += 1
return True
dp = [(1) for _ in range(len(words))]
ans = 1
for i in range(1, len(words)):
j = i - 1
while j >= 0 and len(words[j]) + 1 >= len(words[i]):
val = stringmatch(words[j], words[i])
if val:
print(words[j], words[i])
print(dp[i])
dp[i] = max(dp[j] + 1, dp[i])
ans = max(dp[i], ans)
j -= 1
print(dp)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words.sort(key=len)
dp = [(1) for _ in range(len(words))]
start = collections.defaultdict(int)
end = collections.defaultdict(int)
for i in range(len(words) - 1, -1, -1):
start[len(words[i])] = i
if len(start) == 1:
return 1
for i in range(len(words)):
end[len(words[i])] = i
res = 1
for i in range(start[len(words[0]) + 1], len(words)):
for j in range(start[len(words[i]) - 1], end[len(words[i]) - 1] + 1):
charA = set(words[j])
charB = set(words[i])
intersect = len(charA.intersection(charB))
if intersect == len(charA):
dp[i] = max(dp[i], dp[j] + 1)
res = max(res, dp[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def differ(self, x1, x2):
i, j = 0, 0
diff = 0
while i != len(x1) and j != len(x2):
if x1[i] != x2[j]:
diff += 1
else:
i += 1
if diff > 1:
return False
j += 1
return True
def check(self, x1, x2):
if x1 == x2:
return True
if abs(len(x1) - len(x2)) != 1:
return False
else:
if len(x2) > len(x1):
a, b = x1, x2
else:
a, b = x2, x1
return self.differ(a, b)
def longestStrChain(self, words: List[str]) -> int:
words = list(set(words))
lengths = {}
for word in words:
length = len(word)
lengths[length] = lengths.get(length, []) + [word]
lengths = sorted(lengths.items(), key=lambda x: x[0], reverse=True)
value = {}
max_chain = 1
for i, length in enumerate(lengths):
for word in length[1]:
if i == 0:
value[word] = 1
else:
temp_max = 0
for temp_word in lengths[i - 1][1]:
if self.check(word, temp_word):
temp_max = max(temp_max, value[temp_word])
value[word] = 1 + temp_max
max_chain = max(value[word], max_chain)
return max_chain
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
N = len(words)
dp = [1] * N
words.sort(key=len)
print(words)
if N == 1:
return 1
def check(word1, word2):
if len(word2) - len(word1) != 1:
return False
n = len(word1)
for i in range(n):
if word2[:i] + word2[i + 1 :] == word1:
return True
if word2[:-1] == word1:
return True
return False
for i in range(N):
for j in range(i):
if check(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def chain(w1, w2):
m, n = len(w1), len(w2)
if abs(m - n) != 1:
return False
i, j, one = 0, 0, 1
while i < m and j < n:
if w1[i] == w2[j]:
i, j = i + 1, j + 1
elif one:
one, i = 0, i + 1
else:
return False
return True
if not words:
return 0
words.sort(key=lambda x: len(x))
n = len(words)
dp = [1] * n
ans = 1
for i in range(n):
for j in range(i):
if chain(words[i], words[j]):
dp[i] = max(dp[i], dp[j] + 1)
ans = max(ans, dp[i])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
def find(s1, s2):
i = 0
j = 0
count = 0
while i < len(s1) and j < len(s2):
if s1[i] == s2[j]:
i += 1
j += 1
else:
j += 1
count += 1
if i == len(s1):
return True
if count == 1:
return True
return False
class Solution:
def longestStrChain(self, words: List[str]) -> int:
new_words = []
for w in words:
new_words.append((len(w), w))
new_words.sort()
dp = [1] * len(words)
for i in range(len(words)):
for j in range(i + 1, len(words)):
l1, w1 = new_words[i]
l2, w2 = new_words[j]
if l2 - l1 > 1:
break
if l2 - l1 == 1:
diff = find(w1, w2)
if diff == 1:
dp[j] = max(dp[j], dp[i] + 1)
return max(dp)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
wc = {}
for w in words:
wc.setdefault(len(w), [])
wc[len(w)].append(w)
d = {}
for i in sorted(wc.keys()):
if i - 1 not in wc:
for w in wc[i]:
d[w] = 1
else:
for w in wc[i]:
d[w] = 1
for i in range(len(w)):
if w[:i] + w[i + 1 :] in d:
d[w] = max(d[w], d[w[:i] + w[i + 1 :]] + 1)
return max(d.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def __init__(self):
self.cache = collections.defaultdict(int)
def longestEndWith(self, word, words):
if word in self.cache:
return self.cache[word]
if len(word) == 1:
self.cache[word] = 1
return 1
else:
for i in range(len(word)):
s = word[:i] + word[i + 1 :]
if s in words:
self.cache[word] = max(
self.cache[word], self.longestEndWith(s, words)
)
self.cache[word] += 1
return self.cache[word]
def longestStrChain(self, words: List[str]) -> int:
ans = 0
for word in words:
ans = max(ans, self.longestEndWith(word, words))
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words = sorted(words, key=lambda w: len(w))
seenCache = {}
max_len = 0
for cur_word in words:
cur_len = 0
for i in range(len(cur_word)):
check_word = cur_word[:i] + cur_word[i + 1 :]
cur_len = max(cur_len, seenCache.get(check_word, 0) + 1)
seenCache[cur_word] = cur_len
max_len = max(max_len, cur_len)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def isPre(st1, st2):
if len(st2) != len(st1) + 1:
return False
j = 0
diff = 0
for i in range(len(st2)):
if diff > 1:
return False
elif j < len(st1) and st1[j] == st2[i]:
j += 1
else:
diff += 1
return diff <= 1
words.sort(key=lambda x: len(x))
n = len(words)
dp = [1] * n
for i in range(1, n):
for j in range(i):
if isPre(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, wordlist: List[str]) -> int:
wl = set(wordlist)
def dfs(w):
if len(w) == 1:
return 1
if w in memo:
return memo[w]
l = len(w)
depth = 0
for i in range(l):
next = w[0:i] + w[i + 1 : l]
if next in wordlist:
depth = max(depth, dfs(next))
memo[w] = depth + 1
return depth + 1
memo = {}
longest = 1
for word in wordlist:
longest = max(longest, dfs(word))
return longest
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
ans, dicti, memo = (
0,
collections.defaultdict(list),
collections.defaultdict(int),
)
for word in words:
dicti[len(word)].append(word)
chars = [chr(ch) for ch in range(ord("a"), ord("z") + 1)]
def find(count, word):
if word in memo:
return count + memo[word]
if len(word) + 1 not in dicti:
memo[word] = 0
return count
maxi = 0
for i in range(len(word) + 1):
for ch in chars:
new_word = word[:i] + ch + word[i:]
if new_word in dicti[len(word) + 1]:
maxi = max(maxi, find(1, new_word))
memo[word] = maxi
return count + memo[word]
for word in sorted(words, key=lambda x: len(x)):
el = find(1, word)
ans = max(ans, el)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
self.memo = {}
self.ans = 1
self.words = set(words)
for word in self.words:
self.dfs(word)
return self.ans
def dfs(self, word):
if len(word) == 1:
self.memo[word] = 1
return 1
self.memo[word] = 1
for i in range(len(word)):
nxt = word[:i] + word[i + 1 :]
if nxt in self.words:
self.memo[word] = max(self.memo[word], 1 + self.dfs(nxt))
self.ans = max(self.ans, self.memo[word])
return self.memo[word]
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def __init__(self):
self.results = []
self.explored = dict()
def traverse(self, string, listt, buffer):
flag = True
for i in range(len(string)):
if string[:i] + string[i + 1 :] in listt:
flag = False
self.traverse(
string[:i] + string[i + 1 :],
listt,
buffer + [string[:i] + string[i + 1 :]],
)
if flag:
self.results.append(buffer)
for item in buffer:
self.explored[item] = True
return
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len, reverse=True)
for word in words:
if word not in self.explored:
self.traverse(word, words, [word])
return len(max(self.results, key=len))
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words.sort(key=lambda x: len(x))
counter = [collections.Counter(word) for word in words]
dp = [1] * len(words)
maxlen = 0
for i in range(len(dp)):
for j in range(i):
if (
len(words[i]) - len(words[j]) == 1
and len(counter[i] - counter[j]) == 1
):
dp[i] = max(dp[i], 1 + dp[j])
maxlen = max(maxlen, dp[i])
return maxlen
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
for w in sorted(words, key=len):
dp[w] = max(dp.get(w[:i] + w[i + 1 :], 0) + 1 for i in range(len(w)))
return max(dp.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words.sort(key=lambda x: len(x))
visited = set()
res = 1
for i in range(len(words)):
if words[i] in visited:
continue
visited.add(words[i])
res = max(res, self.dfs(words, words[i], i + 1, visited))
return res
def dfs(self, words, cur, start, visited):
if start >= len(words):
return 1
ma = 1
for i in range(start, len(words)):
if len(words[i]) > len(cur) + 1:
break
if len(words[i]) == len(cur):
continue
if not self.isPredecessor(cur, words[i]):
continue
visited.add(words[i])
ma = max(ma, 1 + self.dfs(words, words[i], i + 1, visited))
return ma
def isPredecessor(self, w1, w2):
for i in range(len(w1)):
if w1[:i] == w2[:i] and w1[i:] == w2[i + 1 :]:
return True
if w1 == w2[:-1]:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def isPredecessor(self, word1, word2):
for i in range(1, len(word2) + 1):
if word2[: i - 1] + word2[i:] == word1:
return True
return False
def longestStrChain(self, words: List[str]) -> int:
wordLengths = {}
for word in words:
if len(word) in wordLengths:
wordLengths[len(word)].append(word)
else:
wordLengths[len(word)] = [word]
longestStrChains = {}
totalMaxLength = 1
for length in sorted(wordLengths.keys()):
for word in wordLengths[length]:
if len(word) - 1 not in wordLengths:
longestStrChains[word] = 1
else:
maxLength = 1
for smallerWord in wordLengths[len(word) - 1]:
if self.isPredecessor(smallerWord, word):
if maxLength < longestStrChains[smallerWord] + 1:
maxLength = longestStrChains[smallerWord] + 1
longestStrChains[word] = maxLength
if totalMaxLength < maxLength:
totalMaxLength = maxLength
print(wordLengths)
print(longestStrChains)
return totalMaxLength
|
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def check(s, t):
m, n = len(s), len(t)
flag = False
j = 0
if n != m + 1:
return False
for i in range(n):
if j < m and t[i] == s[j]:
j += 1
elif not flag:
flag = True
else:
return False
return True
words = sorted(words, key=lambda entry: len(entry), reverse=False)
table = [1] * len(words)
for i in range(1, len(table)):
maxVal = 0
for j in range(i):
if (
len(words[j]) + 1 == len(words[i])
and check(words[j], words[i])
and table[i] < table[j] + 1
):
table[i] = table[j] + 1
return max(table)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
preToPost = collections.defaultdict(set)
for word1 in words:
for word2 in words:
if self.isPredecessor(word1, word2):
preToPost[word1].add(word2)
ans = 0
memo = {}
for word in words:
ans = max(ans, self.dfs(word, preToPost, memo))
return ans
def dfs(self, word, preToPost, memo):
if word in memo:
return memo[word]
ans = 1
for post in preToPost[word]:
ans = max(ans, 1 + self.dfs(post, preToPost, memo))
memo[word] = ans
return memo[word]
def isPredecessor(self, word1, word2):
if not len(word1) + 1 == len(word2):
return False
missed = False
i = 0
j = 0
while i < len(word1) and j < len(word2) and word1[i] == word2[j]:
i += 1
j += 1
j += 1
while i < len(word1) and j < len(word2) and word1[i] == word2[j]:
i += 1
j += 1
return i == len(word1) and j == len(word2)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def if_pred(a, b):
if len(a) + 1 != len(b):
return False
i = 0
j = 0
while i < len(a):
if a[i] == b[j]:
i += 1
j += 1
if j - i > 1:
return False
else:
return True
words.sort(key=lambda x: len(x))
dp = [(1) for _ in range(len(words))]
for i in range(len(words)):
for j in range(i - 1, -1, -1):
if if_pred(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
print(dp)
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
ans = defaultdict(int)
words.sort(key=len)
for s in words:
n = len(s)
now = 0
for i in range(n):
s1, s2 = s[:i], s[i + 1 :]
now = max(now, ans[s1, s2])
now += 1
for i in range(n + 1):
s1, s2 = s[:i], s[i:]
ans[s1, s2] = now
return max(ans.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
longestchain = defaultdict(int)
words.sort(key=len, reverse=True)
for word in words:
for i in range(len(word)):
pred = word[:i] + word[i + 1 :]
longestchain[pred] = max(longestchain[pred], 1 + longestchain[word])
return max(longestchain.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def precedent(w1_c, w2_c):
if len(w1_c) > len(w2_c):
return False
count = 0
for c in w2_c:
if c in w1_c:
count += w2_c[c] - w1_c[c]
else:
count += w2_c[c]
return count == 1
minL = float("inf")
maxL = float("-inf")
longest = 1
len_count = collections.defaultdict(list)
subsequent_of_word = collections.defaultdict(int)
count_of_word = collections.defaultdict(Counter)
for w in words:
len_count[len(w)].append(w)
subsequent_of_word[w] = 1
count_of_word[w] = Counter(w)
maxL = max(maxL, len(w))
minL = min(minL, len(w))
for i in range(maxL - 1, minL - 1, -1):
if i in len_count:
if i + 1 in len_count:
for w1 in len_count[i]:
for w2 in len_count[i + 1]:
if precedent(count_of_word[w1], count_of_word[w2]):
subsequent_of_word[w1] = max(
subsequent_of_word[w1], subsequent_of_word[w2] + 1
)
longest = max(longest, subsequent_of_word[w1])
else:
for w in len_count[i]:
subsequent_of_word[w] = 1
return longest
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
words = [(len(word), word) for word in words]
words.sort()
words = [word[1] for word in words]
dp = [(1) for i in range(n)]
for i in range(n):
for j in reversed(list(range(i))):
if len(words[j]) < len(words[i]) - 1:
break
if len(words[j]) == len(words[i]):
continue
if self.check(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
def check(self, s1, s2):
i, j = 0, 0
count = 0
while i < len(s1) and j < len(s2):
if s1[i] == s2[j]:
i += 1
j += 1
else:
j += 1
count += 1
return i == len(s1)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
word_dict = {}
words = [(len(w), w) for w in words]
words = sorted(words)
for _, w in words:
word_dict[w] = 1
state_t = set()
max_chain = 1
for _, word in words:
chain_len = word_dict[word]
max_chain = max(max_chain, chain_len)
for i in range(len(word) + 1):
for c in range(97, 123):
new_word = word[:i] + chr(c) + word[i:]
if word_dict.get(new_word, False) and new_word not in state_t:
state_t.add(new_word)
word_dict[new_word] = chain_len + 1
return max_chain
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def one_off(self, a, b):
if abs(len(a) - len(b)) != 1:
return False
one_off = False
i = 0
j = 0
while i < len(a) and j < len(b):
if a[i] != b[j]:
if one_off:
return False
one_off = True
if len(a) > len(b):
j -= 1
else:
i -= 1
i += 1
j += 1
return True
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
words.sort(key=lambda word: -len(word))
dp = {}
best = 1
for i, word in enumerate(words):
for other in words[i + 1 :]:
if len(word) - 1 > len(other):
break
if self.one_off(word, other):
dp[other] = max(dp.get(other, 0), dp.get(word, 1) + 1)
best = max(best, dp[other])
return best
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
cache = {}
words_dict = {word: None for word in words}
def chain(word):
if word in cache:
return cache[word]
if word in words:
cache[word] = 1 + max(
map(
chain,
[(word[:idx] + word[idx + 1 :]) for idx in range(len(word))],
)
)
return cache[word]
return 0
return max(map(chain, words))
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NONE VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
counter = {}
minLen = len(words[0])
maxLen = 1
for idx in range(len(words)):
if len(words[idx]) == minLen:
counter[idx] = 1
else:
ancestor = 1
j = 0
while idx - j >= 0 and len(words[idx - j]) >= len(words[idx]) - 1:
j += 1
if len(words[idx - j]) == len(words[idx]):
continue
if self.checker(words[idx - j], words[idx]):
ancestor = max(ancestor, counter[idx - j] + 1)
counter[idx] = ancestor
maxLen = max(maxLen, ancestor)
return maxLen
def checker(self, short, long):
for idx in range(len(long)):
removed = long[0:idx] + long[idx + 1 :]
if short == removed:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dictByLen = collections.defaultdict(list)
wordCounter = {}
for i, w in enumerate(words):
dictByLen[len(w)].append(w)
wordCounter[w] = collections.Counter(w)
def isOneDiff(s1, s2):
diff = 0
j = 0
for c in s2:
if c != s1[j]:
if diff == 1:
return False
diff += 1
else:
j += 1
if j == len(s1):
return True
return True
sortedLens = sorted(dictByLen.keys())
longestLen = 1
prevLen = -1
for l in sortedLens:
if prevLen == -1 or l != prevLen + 1:
prevArr = [(s, 1) for s in dictByLen[l]]
else:
newArr = []
for s2 in dictByLen[l]:
diff = 1
for s1, lsf in prevArr:
if isOneDiff(s1, s2):
diff = max(diff, lsf + 1)
newArr.append((s2, diff))
longestLen = max(diff, longestLen)
prevArr = newArr
prevLen = l
return longestLen
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
if len(words) == 0:
return 0
adj = {}
for w in words:
for i in range(len(w)):
s = w[0:i] + w[i + 1 :]
if s not in adj:
adj[s] = [w]
else:
adj[s].append(w)
res = 1
queue = []
visited = {}
def dfs(root):
if root in visited:
return visited[root]
if root not in adj:
visited[root] = 1
return 1
distance = 1
for next_ in adj[root]:
distance = max(distance, dfs(next_) + 1)
visited[root] = distance
return distance
for w in words:
length = dfs(w)
res = max(res, length)
return res
|
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
_max = 0
def longestStrChain(self, words: List[str]) -> int:
def dfs(cur, step):
if cur not in s:
return
if len(cur) == 0:
return
self._max = max(step, self._max)
for i in range(len(cur)):
t = cur[:i] + cur[i + 1 :]
dfs(t, step + 1)
s = set(words)
for w in sorted(words, key=len, reverse=True):
dfs(w, 1)
return self._max
|
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN IF FUNC_CALL VAR VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
x = lambda a: len(a)
words.sort(key=x)
print(words)
chain_len = [None] * len(words)
chain_len[0] = 1
for i in range(1, len(words)):
temp = words[i]
chain_len[i] = 1
for j in range(len(temp)):
temp2 = temp[:j] + temp[j + 1 :]
if temp2 not in words:
continue
for k in range(i - 1, -1, -1):
if len(words[k]) < len(words[i]) - 1:
break
elif len(words[k]) == len(words[i]) - 1:
if words[k] == temp2:
chain_len[i] = max(chain_len[i], chain_len[k] + 1)
break
return max(chain_len)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
by_length = collections.defaultdict(set)
for w in words:
by_length[len(w)].add(w)
for i, l in enumerate(sorted(by_length.keys())):
if i == 0:
for w in by_length[l]:
dp[w] = 1
elif not by_length[l - 1]:
for w in by_length[l]:
dp[w] = 1
else:
for w in by_length[l]:
dp[w] = 1
for j in range(len(w)):
candidate = w[:j] + w[j + 1 :]
if candidate in by_length[l - 1]:
dp[w] = max(dp[w], dp[candidate] + 1)
return max(dp.values())
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def custom_equal(word1, word2):
if len(word2) == len(word1):
return 0
for i in range(len(word2)):
if word2[0:i] + word2[i + 1 :] == word1:
return 1
return 0
words.sort(key=lambda x: len(x))
max_len = 1
dp = [(1) for _ in words]
for i in range(1, len(words)):
for j in range(i - 1, -1, -1):
if len(words[j]) + 1 < len(words[i]):
break
if custom_equal(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
max_len = max(max_len, dp[i])
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def predecessor(word1, word2):
for i in range(len(word2)):
if word2[0:i] + word2[i + 1 :] == word1:
return True
return False
words.sort(key=len)
words_sort = {}
for i in words:
if len(i) in words_sort:
words_sort[len(i)].append(i)
else:
words_sort[len(i)] = [i]
def dfs(words, words_sort):
max_chain = 1
stack = [(1, word) for word in words[::-1]]
while stack:
current = stack.pop()
if current[0] > max_chain:
max_chain = current[0]
if len(current[1]) + 1 in words_sort:
for word in words_sort[len(current[1]) + 1]:
if predecessor(current[1], word):
if (1, word) in stack:
stack.remove((1, word))
stack.append((current[0] + 1, word))
else:
continue
return max_chain
max_chain = dfs(words, words_sort)
return max_chain
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR IF NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
chainLen = {w: (1) for w in words}
ret = 1
for i, word1 in enumerate(words):
for j in range(i + 1, len(words)):
word2 = words[j]
if len(word2) == len(word1):
continue
if len(word2) > len(word1) + 1:
break
insert = False
match = True
idx1 = 0
idx2 = 0
while idx1 < len(word1):
if word1[idx1] != word2[idx2]:
if insert:
match = False
break
else:
insert = True
idx2 += 1
continue
idx1 += 1
idx2 += 1
if match:
l = max(chainLen[word2], chainLen[word1] + 1)
ret = max(ret, l)
chainLen[word2] = l
return ret
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words_sort = [(len(word), word) for word in words]
words_sort.sort()
words = [word for w_len, word in words_sort]
def isPredecessor(pre: str, succ: str) -> bool:
if len(pre) != len(succ) - 1:
return False
pre = pre + "."
d = 0
for i in range(len(succ)):
if pre[i - d] != succ[i]:
d += 1
if d >= 2:
return False
return True
chain = [1] * len(words)
ans = 1
for i in range(len(words)):
for j in range(0, i):
if isPredecessor(words[j], words[i]):
chain[i] = max(chain[i], chain[j] + 1)
ans = max(ans, chain[i])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
wordSet = set(words)
i_min = min(wordSet)
@lru_cache()
def aux(word):
if not word or word not in wordSet:
return 0
if len(word) == i_min:
return 1
return 1 + max(aux(word[:i] + word[i + 1 :]) for i in range(len(word)))
return max(aux(w) for w in words)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
wordsListLength = len(words)
if wordsListLength <= 1:
return wordsListLength
def isPredecessor(word1, word2):
for i in range(len(word2)):
if word2[:i] + word2[i + 1 :] == word1:
return True
return False
words.sort(key=lambda x: len(x))
result = 1
dp = [(1) for _ in range(wordsListLength)]
for i in range(wordsListLength):
for j in range(i + 1, wordsListLength):
if len(words[i]) + 1 == len(words[j]) and isPredecessor(
words[i], words[j]
):
dp[j] = dp[i] + 1
result = max(result, dp[j])
return result
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def determine(s1, s2):
if len(s2) != len(s1) + 1:
return False
insert = False
j = 0
for i in range(len(s1)):
while s2[j] != s1[i]:
if insert:
return False
j = j + 1
insert = True
j = j + 1
return True
d = {}
words = sorted(words, key=lambda x: len(x))
d[words[-1]] = 1
for i in reversed(list(range(len(words) - 1))):
m = 0
d[words[i]] = 1
for j in range(i, len(words)):
if determine(words[i], words[j]):
if d[words[j]] > m:
d[words[i]] = d[words[j]] + 1
m = d[words[i]]
out = 0
for k in d:
if d[k] > out:
out = d[k]
return out
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
d = {w: (1) for w in words}
words.sort(key=lambda x: len(x))
def check(w1, w2):
if len(w1) <= len(w2):
return False
if len(w1) - len(w2) == 1:
i = 0
j = 0
count = 0
while i < len(w1) and j < len(w2):
if w1[i] == w2[j]:
i += 1
j += 1
else:
i += 1
count += 1
if count > 1:
return False
else:
return True
return False
dp = [(0) for _ in range(len(words))]
dp[0] = 1
res = 1
for i in range(1, len(words)):
curr_max = 0
for j in range(i):
if check(words[i], words[j]):
curr_max = max(curr_max, dp[j])
dp[i] = curr_max + 1
res = max(res, dp[i])
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
self._len_to_words = collections.defaultdict(list)
for w in words:
self._len_to_words[len(w)].append(w)
self._cache = {}
self._visited = {}
max_so_far = 0
for w in words:
max_so_far = max(max_so_far, self._find_longest(w))
return max_so_far
def _find_longest(self, w):
if w in self._visited:
return self._visited[w]
len_w = len(w)
if len_w + 1 not in self._len_to_words:
self._visited[w] = 1
return 1
max_chain_len = 1
for cand in self._len_to_words[len_w + 1]:
if self.is_predecessor(w, cand):
max_chain_len = max(max_chain_len, 1 + self._find_longest(cand))
self._visited[w] = max_chain_len
return max_chain_len
def is_predecessor(self, w1, w2):
if (w1, w2) in self._cache:
return self._cache[w1, w2]
is_p = True
if len(w1) + 1 != len(w2):
is_p = False
else:
count = {}
for c in w1:
if c in count:
count[c] += 1
else:
count[c] = 1
found_diff = False
for c in w2:
if c in count:
count[c] -= 1
if count[c] == 0:
del count[c]
elif found_diff:
is_p = False
break
else:
found_diff = True
count[c] = 1
if is_p:
keys = list(count.keys())
is_p = len(keys) == 1 and count[keys[0]] == 1
self._cache[w1, w2] = is_p
return is_p
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
dp = [1] * len(words)
possible_pred = [0] * len(words)
for i in range(len(words) - 1, -1, -1):
cur_word = words[i]
cur_length = len(cur_word)
for j in range(i + 1, len(words)):
if len(words[j]) == cur_length + 1 and cur_word in possible_pred[j]:
dp[i] = max(dp[i], dp[j] + 1)
pred = set()
for k in range(len(cur_word)):
pred.add(cur_word[0:k] + cur_word[k + 1 :])
possible_pred[i] = pred
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
table = defaultdict(list)
for w in words:
table[len(w)].append(w)
max_len = max(len(w) for w in words)
def backtracking(cur, n):
if len(cur) == max_len:
return 0
if not cur in dp:
res = 0
for word in table[n]:
for i in range(len(word)):
if word[0:i] + word[i + 1 :] == cur:
res = max(res, backtracking(word, n + 1) + 1)
break
dp[cur] = res
return dp[cur]
res = 0
for i, v in table.items():
for w in v:
res = max(res, backtracking(w, i + 1) + 1)
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = [(1) for i in range(len(words))]
words.sort(key=lambda x: len(x))
for i in range(len(words)):
for j in range(i):
if len(words[j]) == len(words[i]):
continue
if len(words[i]) - len(words[j]) > 1:
continue
else:
count = 0
for ch in words[j]:
if ch in words[i]:
count += 1
if count == len(words[j]):
dp[i] = dp[j] + 1
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def predecessor(self, larger, smaller):
largerCount = Counter(larger)
for c in smaller:
if c in largerCount:
largerCount[c] -= 1
if largerCount[c] == 0:
del largerCount[c]
mc = largerCount.most_common()
return len(mc) == 1 and mc[0][1] == 1
def longestStrChain(self, words: List[str]) -> int:
lengthToWords = defaultdict(list)
for word in words:
lengthToWords[len(word)].append(word)
wordToCL = defaultdict(lambda: 1)
res = 1
for wordLength in range(16, 1, -1):
if wordLength - 1 not in lengthToWords:
continue
for larger in lengthToWords[wordLength]:
for smaller in lengthToWords[wordLength - 1]:
if self.predecessor(larger, smaller):
wordToCL[smaller] = max(wordToCL[smaller], wordToCL[larger] + 1)
res = max(res, wordToCL[smaller])
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR FOR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
n = len(words)
dp = [1] * n
for i in range(1, n):
word = words[i]
word_len = len(word)
for j in range(0, i):
if len(words[j]) == word_len - 1:
flag = False
for t in range(word_len):
if word[:t] + word[t + 1 :] == words[j]:
flag = True
if flag:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def _check_word(self, wordlong, word2) -> bool:
if len(wordlong) - len(word2) > 1 or len(wordlong) - len(word2) == 0:
return False
for i in range(0, len(wordlong)):
newstr = wordlong[:i] + wordlong[i + 1 :]
if newstr == word2:
return True
return False
def longestStrChain(self, words: List[str]) -> int:
if len(words) == 1:
return words[0]
words = sorted(words, key=len, reverse=True)
m = [1] * len(words)
for i in range(1, len(words)):
for j in range(0, i):
if self._check_word(words[j], words[i]) and m[j] + 1 > m[i]:
m[i] = m[j] + 1
return max(m)
|
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def is_chainable(self, shorter, longer):
for i in range(len(longer)):
if shorter == longer[:i] + longer[i + 1 :]:
return True
return False
def longestStrChain(self, words: List[str]) -> int:
sw = sorted(words, key=lambda x: len(x))
n_words = len(sw)
chains = [1] * n_words
out = 1
for i in range(n_words):
word = sw[i]
for j in range(i + 1, n_words):
next_word = sw[j]
if len(word) == len(next_word):
continue
elif len(word) - len(next_word) != -1:
break
if self.is_chainable(word, next_word):
chains[j] = chains[i] + 1
out = max(out, chains[j])
return out
|
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def dfs(w1, size):
return max([dfs(w2, size + 1) for w2 in graph[w1]], default=size)
graph = collections.defaultdict(list)
for w in words:
graph[len(w)].append(w)
for w1 in words:
for w2 in graph[len(w1) + 1]:
for i in range(len(w2)):
if w2[:i] + w2[i + 1 :] == w1:
graph[w1].append(w2)
return max(dfs(w, 1) for w in words)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
contains = lambda w1, w2: all(l1 in w2 for l1 in w1)
words.sort(key=lambda a: len(a))
n = len(words)
chain = [1] * n
for i in range(n):
j = i + 1
while j < n and len(words[j]) == len(words[i]):
j += 1
while j < n and len(words[j]) == len(words[i]) + 1:
if contains(words[i], words[j]) and chain[i] + 1 > chain[j]:
chain[j] = chain[i] + 1
j += 1
return max(chain)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
by_length = collections.defaultdict(set)
for word in words:
by_length[len(word)].add(word)
longest = 1
seen = {*()}
mx = len(by_length)
mn = min(by_length)
for length in sorted(by_length, reverse=True):
if length - mn < longest:
break
for word in by_length[length]:
if length - mn < longest:
break
if word in seen:
continue
stk = [(word, length, 1)]
while stk:
word, k, n = stk.pop()
seen.add(word)
if n > longest:
longest = n
for i in range(k):
pre = word[:i] + word[i + 1 :]
if pre not in seen and pre in by_length[k - 1]:
stk.append((pre, k - 1, n + 1))
if longest == mx:
return longest
return longest
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
max_len = [1] * len(words)
res = 1
for i in range(len(words) - 1):
for j in range(i, len(words)):
if self.isPredecessor(words[i], words[j]):
max_len[j] = max(max_len[i] + 1, max_len[j])
res = max(res, max_len[j])
return res
def isPredecessor(self, s1, s2):
if len(s2) - len(s1) != 1:
return False
flag = 0
i, j = 0, 0
while i < len(s1) and j < len(s2):
if s1[i] == s2[j]:
i += 1
j += 1
elif flag == 0:
flag = 1
j += 1
else:
return False
return True
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
word_graph = {}
for word in words:
for second_word in words:
if len(second_word) == len(word) + 1:
is_predecessor = True
second_word_counter = Counter(second_word)
first_word_counter = Counter(word)
for key, value in list(first_word_counter.items()):
if second_word_counter[key] < value:
is_predecessor = False
break
if is_predecessor:
if word in word_graph:
word_graph[word].append(second_word)
else:
word_graph[word] = [second_word]
max_level = 1
for key, value in list(word_graph.items()):
queue = deque()
queue.append((key, 1))
visted = set()
while queue:
curr, level = queue.popleft()
if level > max_level:
max_level = level
if curr in word_graph:
for next_word in word_graph[curr]:
queue.append((next_word, level + 1))
return max_level
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def is_predecessor(w1, w2):
for i, w in enumerate(w1):
if w != w2[i]:
return w1[i:] == w2[i + 1 :]
return True
words.sort(key=len, reverse=True)
len_words = len(words)
combos = [0] * len_words
for i in range(len_words):
for j in reversed(list(range(0, i))):
len_i, len_j = len(words[i]), len(words[j])
if len_i + 1 < len_j:
break
elif len_i == len_j:
continue
if combos[i] < combos[j] + 1 and is_predecessor(words[i], words[j]):
combos[i] = combos[j] + 1
return max(combos) + 1
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
for ele in words:
ele = list(ele)
words = sorted(words, key=lambda x: len(x))
arr = [1] * n
for i in range(1, n):
for j in range(0, i):
if len(words[j]) == len(words[i]) - 1:
if all(x in words[i] for x in words[j]):
arr[i] = max(arr[i], arr[j] + 1)
print(arr)
return max(arr)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def ispre(w1, w2):
i, j = 0, 0
while i < len(w1) and j < len(w2):
if w1[i] == w2[j]:
i += 1
j += 1
else:
j += 1
return i == len(w1)
words.sort(key=len)
dp = [0] * len(words)
def dfs(i):
if dp[i] != 0:
return dp[i]
dp[i] = 1
for j in range(i, len(words)):
if len(words[j]) <= len(words[i]):
continue
if len(words[j]) > len(words[i]) + 1:
break
if ispre(words[i], words[j]):
dp[i] = max(dp[i], 1 + dfs(j))
return dp[i]
ans = max(dfs(i) for i in range(len(words)))
return ans
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
cnt = [[] for i in range(16)]
min_l = 16
for i in words:
min_l = min([min_l, len(i)])
cnt[len(i) - 1].append(i)
def match(a, b):
i = j = 0
cnt = 0
while j < len(b):
if i < len(a) and a[i] == b[j]:
i += 1
j += 1
else:
j += 1
cnt += 1
return cnt == 1
def topdown(curr_words):
if len(curr_words) == 16:
return 1
if curr_words in dp:
return dp[curr_words]
dp[curr_words] = 1
for i in cnt[len(curr_words)]:
if match(curr_words, i):
if not i in dp:
dp[i] = topdown(i) + 1
dp[curr_words] = max([dp[curr_words], dp[i]])
return dp[curr_words]
ans = 0
for i in range(min_l - 1, 16):
for j in cnt[i]:
if j in dp:
ans = max([ans, dp[j]])
else:
ans = max([ans, topdown(j)])
return ans
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
W = defaultdict(list)
for i, w in enumerate(words):
W[len(w)].append(i)
G = defaultdict(set)
for i in range(n):
for j in W[len(words[i]) + 1]:
found = False
for k in range(len(words[j])):
if words[i] == words[j][:k] + words[j][k + 1 :]:
found = True
break
if found:
G[i].add(j)
@lru_cache(None)
def dfs(i):
out = 1
for j in G[i]:
out = max(out, 1 + dfs(j))
return out
return max(dfs(i) for i in range(n))
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def predecessor(self, word_1, word_2):
if abs(len(word_1) - len(word_2)) != 1:
return False
idx_1 = 0
idx_2 = 0
while idx_1 < len(word_1) and idx_2 < len(word_2):
if word_1[idx_1] != word_2[idx_2]:
if idx_1 != idx_2:
return False
if len(word_1) < len(word_2):
idx_2 += 1
else:
idx_1 += 1
else:
idx_1 += 1
idx_2 += 1
return True
def longestStrChain(self, words: List[str]) -> int:
dict = {}
dp = [(1) for i in range(0, len(words))]
words.sort(key=len)
for i in range(0, len(words)):
words[i] = "".join(sorted(words[i]))
if len(words[i]) in list(dict.keys()):
dict[len(words[i])].append(i)
else:
dict[len(words[i])] = [i]
for i in range(0, len(words)):
if len(words[i]) + 1 in list(dict.keys()):
for elem in dict[len(words[i]) + 1]:
if self.predecessor(words[elem], words[i]):
dp[elem] = max(dp[elem], dp[i] + 1)
ans = 0
for i in range(0, len(dp)):
ans = max(ans, dp[i])
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
predecessors = dict()
words = sorted(words, key=len)
for i in range(len(words)):
predecessors[words[i]] = []
for j in range(i):
if self.isPredecessor(words[i], words[j]):
predecessors[words[i]].append((words[j], j))
T = [0] * len(words)
T[0] = 1
for i in range(1, len(words)):
if predecessors[words[i]]:
T[i] = max([T[j[1]] for j in predecessors[words[i]]]) + 1
else:
T[i] = 1
return max(T)
def isPredecessor(self, word, candidate):
if len(candidate) + 1 != len(word):
return False
i, j = 0, 0
wildcard = True
while i < len(word) and j < len(candidate):
if word[i] == candidate[j]:
i += 1
j += 1
elif wildcard:
i += 1
wildcard = False
else:
return False
return i + 1 == len(word) or i == len(word) if wildcard else i == len(word)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = [(0) for i in range(len(words))]
ans = 0
words = sorted(words, key=lambda x: len(x))
for i in range(len(words)):
for j in range(i):
if self.prev(words[j], words[i]):
dp[i] = max(dp[i], dp[j] + 1)
ans = max(ans, dp[i])
return ans + 1
def prev(self, word_1, word_2):
len_1, len_2 = len(word_1), len(word_2)
if len_1 + 1 != len_2:
return False
i = j = 0
while i < len_1 and j < len_2:
if word_1[i] == word_2[j]:
i += 1
j += 1
if i == len_1:
return True
else:
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
sorted(words, key=len, reverse=True)
depth = [1] * len(words)
def dfs(word):
depth = 1
for i in range(len(word)):
new_word = word[:i] + word[i + 1 :]
if new_word in words:
depth = max(dfs(new_word) + 1, depth)
seen.append(word)
return depth
count = []
seen = []
for word in words:
if word not in seen:
count.append(dfs(word))
return max(count)
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
def is_pred(short, long):
if len(short) + 1 != len(long):
return False
short += " "
new = None
for i in range(len(long)):
if short[i] != long[i]:
new = i
break
if short[:-1] == long[:new] + long[new + 1 :]:
return True
return False
words.sort(key=lambda x: len(x))
dp = [1] * len(words)
for i in range(1, len(words)):
for j in range(i):
if is_pred(words[j], words[i]):
if dp[j] >= dp[i]:
dp[i] = dp[j] + 1
return max(dp)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
all_chains = []
for word in words:
all_chains.extend(self.createChains(word, words))
return max(map(len, all_chains))
def createChains(self, word, words):
result = []
step = [word]
dictionary = set(words)
self.helper(word, step, dictionary, result)
return result
def helper(self, word, step, dictionary, result):
result.append(step[:])
for i in range(len(word) - 1, -1, -1):
substring = word[:i] + word[i + 1 :]
if substring in dictionary:
step.append(substring)
self.helper(substring, step, dictionary, result)
step.pop()
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
print(words)
n_s = len(words[0])
dic = collections.defaultdict(lambda: 0)
longest_chain = 1
def search(word):
if len(word) == n_s:
dic[word] = 1
return 1
N = 1
for i in range(0, len(word)):
sub = word[0:i] + word[i + 1 :]
if sub in words:
n = search(sub)
N = max(N, n + 1)
dic[word] = N
nonlocal longest_chain
longest_chain = max(longest_chain, N)
return N
for i in range(len(words) - 1, -1, -1):
if dic[words[i]] in dic:
continue
word = words[i]
re = search(word)
longest_chain = max(longest_chain, re)
return longest_chain
|
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def isPredecessor(self, predecessorCandidate, word):
if len(predecessorCandidate) != len(word) - 1:
return False
pWhere = 0
wWhere = 0
hasSkipped = False
while pWhere < len(predecessorCandidate) and wWhere < len(word):
if predecessorCandidate[pWhere] != word[wWhere]:
if hasSkipped:
return False
hasSkipped = True
wWhere += 1
else:
wWhere += 1
pWhere += 1
return True
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=lambda x: len(x))
predecessorDict = collections.defaultdict(list)
chainCountDict = collections.defaultdict(lambda: 1)
currentMax = 1
for i in range(0, len(words)):
for j in range(i + 1, len(words)):
if self.isPredecessor(words[i], words[j]):
predecessorDict[words[j]].append(words[i])
chainCountDict[words[j]] = max(
chainCountDict[words[j]], 1 + chainCountDict[words[i]]
)
currentMax = max(currentMax, chainCountDict[words[j]])
return currentMax
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def IsPredecessor(self, w1, w2):
if len(w1) + 1 != len(w2):
return False
if len(w1) == 0:
return True
diff = 0
for i in range(len(w2)):
if i - diff == len(w1) or w1[i - diff] != w2[i]:
diff += 1
return diff == 1
def longestStrChain(self, words: List[str]) -> int:
if len(words) < 1:
return 0
length_position = [[] for i in range(17)]
for i in range(len(words)):
w = words[i]
length_position[len(w)].append(i)
dp = [1] * len(words)
for i in range(2, 17):
if len(length_position[i - 1]) == 0 or len(length_position[i]) == 0:
continue
current = length_position[i]
previous = length_position[i - 1]
for j in current:
for k in previous:
if self.IsPredecessor(words[k], words[j]):
dp[j] = max(dp[j], dp[k] + 1)
return max(dp)
|
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def isPredecessor(self, word1, word2):
if len(word1) + 1 == len(word2):
i = 0
j = 0
count = 0
while i < len(word1) and j < len(word2):
if word1[i] != word2[j]:
count += 1
else:
i += 1
j += 1
if count > 1:
return False
elif count == 0:
if len(word2) == j + 1:
return True
else:
return True
else:
return False
def memoization(self, i, words, memo):
if i >= len(words):
return 0
if memo[i] is not None:
return memo[i]
length = 0
for j in range(i + 1, len(words)):
if self.isPredecessor(words[i], words[j]):
length = max(length, self.memoization(j, words, memo))
memo[i] = 1 + length
return memo[i]
def longestStrChain(self, words: List[str]) -> int:
words = sorted(words, key=len)
memo = [None] * len(words)
length = 0
for i in range(len(words)):
length = max(length, self.memoization(i, words, memo))
return length
|
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
|
Given a list of words, each word consists of English lowercase letters.
Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".
A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.
Return the longest possible length of a word chain with words chosen from the given list of words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 1000
1 <= words[i].length <= 16
words[i] only consists of English lowercase letters.
|
class Solution:
def longestStrChain(self, words: List[str]) -> int:
st = set(words)
def ls(word):
res = 1
for j in range(len(word)):
substr = word[:j] + word[j + 1 :]
if substr not in st:
continue
res = 1 + ls(substr)
return res
res = 1
for word in words:
res = max(res, ls(word))
return res
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.