Advertisement

Construct String with Minimum Cost - LeetCode 3213 Solution

Construct String with Minimum Cost - Complete Solution Guide

Construct String with Minimum Cost is LeetCode problem 3213, a Hard 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 a string target , an array of strings words , and an integer array costs , both arrays of the same length. Imagine an empty string s . You can perform the following operation any number of times (including zero ): Choose an index i in the range [0, words.length - 1] . Append words[i] to s . The cost of operation is costs[i] . Return the minimum cost to make s equal to target . If it's not possible, return -1 . Example 1: Input: target = "abcdef", words = ["abdef","abc","d","def","e

Detailed Explanation

The problem asks us to construct a target string 'target' by concatenating strings from a given array 'words'. Each word in 'words' has an associated cost in the 'costs' array. The goal is to find the minimum total cost to construct the 'target' string. If it's impossible to construct the 'target' string using the given words, we should return -1.

Solution Approach

The solution combines dynamic programming and a Trie data structure. We use dynamic programming to find the minimum cost to construct each prefix of the target string. The dp array stores the minimum cost to construct the target string up to a certain index. A Trie is used to efficiently search the 'words' array to find possible matches within the target string.

Step-by-Step Algorithm

  1. Step 1: Create a Trie data structure and insert all the words from the 'words' array into the Trie. Before inserting, store the minimum cost for each unique word using a hash map (or an array and lookup for C implementation). This avoids processing the same word multiple times with varying costs.
  2. Step 2: Initialize a DP array 'dp' of size n+1 (where n is the length of the target string) with infinity. dp[0] = 0, representing the cost to create an empty string.
  3. Step 3: Iterate through the target string from index i = 0 to n-1. If dp[i] is infinity, it means that the prefix target[0...i-1] cannot be constructed, so skip to the next iteration.
  4. Step 4: For each index i, traverse the Trie starting from the root. For each subsequent character target[j] from i to n-1, check if the character exists as a child in the current Trie node.
  5. Step 5: If the character exists, move to the child node. If the child node represents the end of a word (i.e., has a cost associated with it), update dp[j+1] to be the minimum of its current value and dp[i] + cost, where cost is the cost of the word.
  6. Step 6: If the character does not exist in the Trie, stop traversing the Trie for the current value of i.
  7. Step 7: After iterating through the target string, dp[n] will contain the minimum cost to construct the entire target string. If dp[n] is still infinity, it means the target string cannot be constructed, so return -1. Otherwise, return dp[n].

Key Insights

  • Insight 1: Dynamic Programming is suitable for this problem because we can build the solution incrementally, considering prefixes of the target string and the minimum cost to construct them.
  • Insight 2: A Trie data structure can efficiently check if a substring of the target string matches any of the given words.
  • Insight 3: We should only consider the minimum cost for each distinct word, to avoid redundant computations. If a word appears multiple times with different costs, take the smallest cost.

Complexity Analysis

Time Complexity: O(n * m), where n is the length of the target string and m is the average length of the words. The nested loop iterates through all possible substrings of the target, and in the worst case, the trie lookup for each substring takes O(m) time.

Space Complexity: O(T + n), where T is the total number of nodes in the Trie and n is the length of the target string. The Trie can store all the words, and the DP array requires O(n) space.

Topics

This problem involves: Array, String, Dynamic Programming, Trie.

Companies

Asked at: Mitsogo.