Advertisement

Longest String Chain - LeetCode 1048 Solution

Longest String Chain - Complete Solution Guide

Longest String Chain is LeetCode problem 1048, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array of words where each word consists of lowercase English letters. word A is a predecessor of word B if and only if we can insert exactly one letter anywhere in word A without changing the order of the other characters to make it equal to word B . For example, "abc" is a predecessor of "ab a c" , while "cba" is not a predecessor of "bcad" . 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

Detailed Explanation

The problem asks us to find the length of the longest possible word chain within a given array of words. A word chain is a sequence of words where each word is a predecessor of the next word. A word A is a predecessor of word B if we can insert exactly one letter into word A to make it equal to word B, without changing the order of other characters. For example, "abc" is a predecessor of "abac". The input is an array of words, and the output is the length of the longest possible word chain that can be formed using words from the input array.

Solution Approach

The solution uses dynamic programming to find the length of the longest word chain. The words are first sorted by their lengths. This makes it possible to build the chain in increasing order of length. A hash map (or dictionary) `dp` is used to store the longest chain ending at a particular word. For each word, it checks all its possible predecessors by removing one character at each possible index. If the predecessor exists in the `dp`, the current word's chain length is updated to be 1 plus the length of the chain ending at the predecessor. The maximum chain length seen so far is tracked and returned.

Step-by-Step Algorithm

  1. Step 1: Sort the input array of words by length in ascending order.
  2. Step 2: Initialize a hash map `dp` to store the length of the longest chain ending at each word. Initialize `max_len` to 0 to keep track of the longest chain found so far.
  3. Step 3: Iterate through each word in the sorted array.
  4. Step 4: For each word, iterate through each possible index `i` from 0 to the length of the word.
  5. Step 5: Create a predecessor by removing the character at index `i` from the current word.
  6. Step 6: Look up the predecessor in the `dp`. If the predecessor exists, update the `current_max` to be the maximum between the existing `current_max` value and the value associated with the predecessor in the `dp`.
  7. Step 7: After checking all possible predecessors, store the length of the longest chain ending at the current word in the `dp` as `1 + current_max`.
  8. Step 8: Update `max_len` to be the maximum of the current `max_len` and the length of the chain ending at the current word.
  9. Step 9: After iterating through all the words, return `max_len`.

Key Insights

  • Insight 1: Sorting words by length allows us to build the chain in a bottom-up manner. We only need to consider words longer than the current word being processed.
  • Insight 2: Dynamic programming is used to efficiently store and reuse the length of the longest chain ending at a particular word. This avoids redundant calculations.
  • Insight 3: Checking if one word is a predecessor of another involves iterating through the longer word and comparing it to the shorter word after skipping one character at a time. This can be optimized by ensuring only one character is skipped.

Complexity Analysis

Time Complexity: O(N*M^2)

Space Complexity: O(N)

Topics

This problem involves: Array, Hash Table, Two Pointers, String, Dynamic Programming, Sorting.

Companies

Asked at: Atlassian, Citadel, Flipkart, MathWorks, Moloco, Snap, Two Sigma, Verily, Wix.