Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Length of longest string chain in JavaScript
A word chain problem involves finding sequences of words where each word can be formed by adding exactly one character to the previous word. This is a classic dynamic programming problem that requires checking predecessor relationships and building chains efficiently.
Understanding Word Chains
A word1 is a predecessor of word2 if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac" because we can insert 'a' at position 2.
A word chain is a sequence of words [word_1, word_2, ..., word_k] where each word is a predecessor of the next word in the sequence.
Problem Statement
Given an array of strings consisting of English lowercase letters, find the longest possible length of a word chain that can be formed using words from the array.
Algorithm Approach
The solution uses dynamic programming with these key steps:
- Sort words by length to process shorter words first
- For each word, check all longer words to find valid successors
- Use memoization to store the maximum chain length ending at each word
Example Implementation
const arr = ["a","b","ba","bca","bda","bdca"];
const longestStrChain = (arr) => {
// Sort by length to process shorter words first
arr.sort((a, b) => a.length - b.length);
// Check if word1 is a predecessor of word2
const isPredecessor = (word1 = '', word2 = '') => {
if(Math.abs(word1.length - word2.length) !== 1){
return false;
}
// Try removing each character from word2 to see if it equals word1
for(let i = 0; i < word2.length; i++){
const word = word2.slice(0, i) + word2.slice(i + 1);
if(word === word1){
return true;
}
}
return false;
};
const dp = [];
let maxChainLength = 0;
// Process words from longest to shortest
for(let i = arr.length - 1; i >= 0; i--){
dp[i] = 1; // Each word forms a chain of length 1
// Check all longer words to extend the chain
for(let j = arr.length - 1; j > i; j--){
if(isPredecessor(arr[i], arr[j])){
dp[i] = Math.max(dp[i], 1 + dp[j]);
}
}
maxChainLength = Math.max(maxChainLength, dp[i]);
}
return maxChainLength;
};
console.log(longestStrChain(arr));
4
How It Works
The algorithm works backwards from the longest words:
- Sort the array by word length
- For each word, initialize its chain length to 1
- Check all longer words to see if they can extend this word's chain
- Update the maximum chain length found so far
For the example input ["a","b","ba","bca","bda","bdca"], one longest chain is: "a" ? "ba" ? "bda" ? "bdca" with length 4.
Time Complexity
The time complexity is O(n² × m) where n is the number of words and m is the average word length. The space complexity is O(n) for the dynamic programming array.
Conclusion
This dynamic programming solution efficiently finds the longest word chain by processing words in length order and using memoization. The key insight is checking predecessor relationships by trying to remove each character from longer words.
