Advertisement

Minimum Cost to Convert String I - LeetCode 2976 Solution

Minimum Cost to Convert String I - Complete Solution Guide

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

Detailed Explanation

The problem asks us to find the minimum cost to convert a string `source` to a string `target` of the same length. We are given a set of possible character changes with associated costs in the `original`, `changed`, and `cost` arrays, respectively. We can apply these changes an unlimited number of times. The goal is to find the cheapest sequence of changes to transform `source` into `target`. If it's impossible to convert `source` to `target`, we return -1. The key constraint is that changes can only occur if the corresponding `original[i]`, `changed[i]`, and `cost[i]` exist in the input arrays.

Solution Approach

The solution utilizes the Floyd-Warshall algorithm to find the shortest paths between all pairs of characters. It first constructs a cost matrix (distance matrix) representing the cost of changing one character to another. Then, it applies the Floyd-Warshall algorithm to this matrix to compute the minimum cost between any two characters. Finally, it iterates through the `source` and `target` strings and sums up the minimum costs to convert each character in `source` to the corresponding character in `target`. If any character conversion is impossible (i.e., the minimum cost is infinite), it returns -1. Otherwise, it returns the total cost.

Step-by-Step Algorithm

  1. Step 1: Initialize a 2D array `dist` of size 26x26 (for lowercase English letters) to represent the minimum cost between any two characters. Initialize all values to infinity, and set the cost of changing a character to itself to 0.
  2. Step 2: Populate the `dist` array with the initial costs from the `original`, `changed`, and `cost` arrays. `dist[u][v]` will store the cost of changing character `u` to character `v`. If there are multiple ways to change from `u` to `v`, take the minimum cost.
  3. Step 3: Apply the Floyd-Warshall algorithm to compute the shortest paths between all pairs of characters. This involves iterating through all possible intermediate nodes `k`, and updating the `dist[i][j]` value if going from `i` to `j` through `k` is cheaper than the current cost.
  4. Step 4: Iterate through the `source` and `target` strings. For each character pair `source[i]` and `target[i]`, find the minimum cost to convert `source[i]` to `target[i]` using the `dist` array.
  5. Step 5: If the minimum cost for any character conversion is infinity, it means the conversion is impossible. Return -1.
  6. Step 6: Otherwise, sum up the minimum costs for all character conversions. Return the total cost.

Key Insights

  • Insight 1: The problem can be modeled as a shortest path problem on a graph where each character is a node, and the cost to change from one character to another is the weight of the edge connecting them.
  • Insight 2: Since the number of characters is limited (lowercase English letters), we can efficiently use the Floyd-Warshall algorithm to compute the shortest paths between all pairs of characters.
  • Insight 3: If the shortest path between two characters is infinite (i.e., there is no path), then the conversion is impossible. We need to check for this condition when calculating the total cost.

Complexity Analysis

Time Complexity: O(n + m)

Space Complexity: O(1)

Topics

This problem involves: Array, String, Graph, Shortest Path.

Companies

Asked at: Atlassian.