Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 1048 Longest String Chain - Walkthrough

LIS on strings

A word chain where each word is the previous plus one letter. Sort words by length, then DP. For each word, try removing each character. If the resulting word exists and has a chain value, extend from it. Example: ["a","ba","bda","bdca"][\text{"a"}, \text{"ba"}, \text{"bda"}, \text{"bdca"}]. Chain: a → ba → bda → bdca. Length 44. Use a hashmap for O(1)O(1) lookup. Time: O(nL2)O(n \cdot L^2)

Time complexity: O(nL2)O(n \cdot L^2) where LL is max word length (for each word, try LL removals, each taking O(L)O(L)).

Space complexity: O(nL)O(n \cdot L) for the hash map.