1129. Longest String Chain
My accepted Python solution to LeetCode problem 1129, Longest String Chain, running in 47ms.
- Difficulty: Medium
- Python
- Runtime 47ms
- Memory 17.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 47ms, memory 17.5MB, accepted 2026-01-02.
class Solution:
def longestStrChain(self, words: List[str]) -> int:
# Sort by length
words.sort(key=len)
# dp[word] = longest chain ending at word
dp = {}
max_len = 1
for word in words:
dp[word] = 1
# Try removing each character
for i in range(len(word)):
predecessor = word[:i] + word[i+1:]
if predecessor in dp:
dp[word] = max(dp[word], dp[predecessor] + 1)
max_len = max(max_len, dp[word])
return max_len