Advertisement

Minimum Cost to Convert String II - LeetCode 2977 Solution

Minimum Cost to Convert String II - Complete Solution Guide

Minimum Cost to Convert String II is LeetCode problem 2977, 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 two 0-indexed strings source and target , both of length n and consisting of lowercase English characters. You are also given two 0-indexed string arrays original and changed , and an integer array cost , where cost[i] represents the cost of converting the string original[i] to the string changed[i] . You start with the string source . In one operation, you can pick a substring x from the string, and change it to y at a cost of z if there exists any index j such that cost[j] == z ,

Detailed Explanation

The problem asks us to find the minimum cost to convert a string `source` to a string `target`. We are given a set of allowed conversions, where converting a substring `original[i]` to `changed[i]` costs `cost[i]`. We can perform multiple conversions as long as the substrings picked are either disjoint or identical. The goal is to return the minimum possible total cost to transform `source` into `target`, or -1 if the transformation is impossible.

Solution Approach

The solution uses a dynamic programming approach combined with the Floyd-Warshall algorithm and a Trie data structure. First, it builds a graph representing the cost to convert one string to another using the given `original`, `changed`, and `cost` arrays. The Floyd-Warshall algorithm is then applied to find the shortest path (minimum cost) between any two strings in the graph. Next, a Trie is constructed to efficiently find all occurrences of convertible substrings in both the `source` and `target` strings. Finally, dynamic programming is used to calculate the minimum cost to convert the `source` string to the `target` string. The `dp[i]` value represents the minimum cost to convert the first `i` characters of `source` to the first `i` characters of `target`.

Step-by-Step Algorithm

  1. Step 1: **Build the Conversion Graph:** Create a graph where nodes represent strings (unique strings from `original` and `changed`). Edges represent the cost to convert one string to another as given in the `cost` array.
  2. Step 2: **Find All-Pairs Shortest Paths (Floyd-Warshall):** Use the Floyd-Warshall algorithm to compute the minimum cost to convert any string to any other string within the graph. This allows us to efficiently determine the cost of converting `original[i]` to `changed[i]` even if there isn't a direct edge.
  3. Step 3: **Construct the Trie:** Build a Trie data structure using the strings from `original` and `changed`. The Trie enables fast substring searching within `source` and `target` strings. Each leaf node stores the index of the string.
  4. Step 4: **Identify Matching Substrings:** Traverse `source` and `target`, using the Trie to identify all possible convertible substrings at each position. Store the indices in source_matches and target_matches where conversion can occur at different length.
  5. Step 5: **Dynamic Programming:** Initialize a DP array `dp` of size `n+1`, where `dp[i]` represents the minimum cost to convert `source[0...i-1]` to `target[0...i-1]`. The base case is `dp[0] = 0`. Iterate from `i = 1` to `n`. For each `i`, consider two cases: (1) If `source[i-1] == target[i-1]`, then `dp[i] = min(dp[i], dp[i-1])` (no operation). (2) For each possible substring ending at `i`, check if a conversion is possible using source_matches and target_matches. If a conversion is possible and has a cost from the Floyd-Warshall result, update `dp[i]` with the minimum cost so far.
  6. Step 6: **Return Result:** After the DP iteration, `dp[n]` contains the minimum cost to convert `source` to `target`. If `dp[n]` is infinity, it means the conversion is impossible, so return -1. Otherwise, return `dp[n]`.

Key Insights

  • Insight 1: The problem's constraint on operations (disjoint or identical substrings) allows for the use of dynamic programming, as the minimum cost to convert a prefix of the string can be computed based on the minimum cost to convert smaller prefixes.
  • Insight 2: Building a graph of conversions between substrings and finding all-pairs shortest paths using the Floyd-Warshall algorithm is crucial for determining the cost of converting between any two given strings.
  • Insight 3: A Trie data structure efficiently identifies all possible substrings of `source` and `target` that can be converted according to the provided `original` and `changed` arrays.

Complexity Analysis

Time Complexity: O(N^3 + M * S^2)

Space Complexity: O(N^2 + T)

Topics

This problem involves: Array, String, Dynamic Programming, Graph, Trie, Shortest Path.

Companies

Asked at: Atlassian.