{"id":10100,"date":"2024-01-08T06:01:15","date_gmt":"2024-01-08T14:01:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10100"},"modified":"2024-01-08T06:01:52","modified_gmt":"2024-01-08T14:01:52","slug":"leetcode-2976-minimum-cost-to-convert-string-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2976-minimum-cost-to-convert-string-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2976. Minimum Cost to Convert String I"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;strings&nbsp;<code>source<\/code>&nbsp;and&nbsp;<code>target<\/code>, both of length&nbsp;<code>n<\/code>&nbsp;and consisting of&nbsp;<strong>lowercase<\/strong>&nbsp;English letters. You are also given two&nbsp;<strong>0-indexed<\/strong>&nbsp;character arrays&nbsp;<code>original<\/code>&nbsp;and&nbsp;<code>changed<\/code>, and an integer array&nbsp;<code>cost<\/code>, where&nbsp;<code>cost[i]<\/code>&nbsp;represents the cost of changing the character&nbsp;<code>original[i]<\/code>&nbsp;to the character&nbsp;<code>changed[i]<\/code>.<\/p>\n\n\n\n<p>You start with the string&nbsp;<code>source<\/code>. In one operation, you can pick a character&nbsp;<code>x<\/code>&nbsp;from the string and change it to the character&nbsp;<code>y<\/code>&nbsp;at a cost of&nbsp;<code>z<\/code>&nbsp;<strong>if<\/strong>&nbsp;there exists&nbsp;<strong>any<\/strong>&nbsp;index&nbsp;<code>j<\/code>&nbsp;such that&nbsp;<code>cost[j] == z<\/code>,&nbsp;<code>original[j] == x<\/code>, and&nbsp;<code>changed[j] == y<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;cost to convert the string&nbsp;<\/em><code>source<\/code><em>&nbsp;to the string&nbsp;<\/em><code>target<\/code><em>&nbsp;using&nbsp;<strong>any<\/strong>&nbsp;number of operations. If it is impossible to convert<\/em>&nbsp;<code>source<\/code>&nbsp;<em>to<\/em>&nbsp;<code>target<\/code>,&nbsp;<em>return<\/em>&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that there may exist indices&nbsp;<code>i<\/code>,&nbsp;<code>j<\/code>&nbsp;such that&nbsp;<code>original[j] == original[i]<\/code>&nbsp;and&nbsp;<code>changed[j] == changed[i]<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> source = \"abcd\", target = \"acbe\", original = [\"a\",\"b\",\"c\",\"c\",\"e\",\"d\"], changed = [\"b\",\"c\",\"b\",\"e\",\"b\",\"e\"], cost = [2,5,5,1,2,20]\n<strong>Output:<\/strong> 28\n<strong>Explanation:<\/strong> To convert the string \"abcd\" to string \"acbe\":\n- Change value at index 1 from 'b' to 'c' at a cost of 5.\n- Change value at index 2 from 'c' to 'e' at a cost of 1.\n- Change value at index 2 from 'e' to 'b' at a cost of 2.\n- Change value at index 3 from 'd' to 'e' at a cost of 20.\nThe total cost incurred is 5 + 1 + 2 + 20 = 28.\nIt can be shown that this is the minimum possible cost.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> source = \"aaaa\", target = \"bbbb\", original = [\"a\",\"c\"], changed = [\"c\",\"b\"], cost = [1,2]\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> source = \"abcd\", target = \"abce\", original = [\"a\"], changed = [\"e\"], cost = [10000]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= source.length == target.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>source<\/code>,&nbsp;<code>target<\/code>&nbsp;consist of lowercase English letters.<\/li><li><code>1 &lt;= cost.length == original.length == changed.length &lt;= 2000<\/code><\/li><li><code>original[i]<\/code>,&nbsp;<code>changed[i]<\/code>&nbsp;are lowercase English letters.<\/li><li><code>1 &lt;= cost[i] &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>original[i] != changed[i]<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: All pairs shortest path<\/strong><\/h2>\n\n\n\n<p>Compute the shortest path (min cost) for any given letter pairs.<\/p>\n\n\n\n<p>Time complexity: O(26^3 + m + n)<br>Space complexity: O(26^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n    long long minimumCost(string s, string t, vector<char>& o, vector<char>& c, vector<int>& cost) {\n    const int n = c.size();\n    vector<vector<int>> d(26, vector<int>(26, 1e9));\n    for (int i = 0; i < 26; ++i)\n      d[i][i] = 0;\n    for (int i = 0; i < n; ++i)\n      d[o[i] - 'a'][c[i] - 'a'] = min(d[o[i] - 'a'][c[i] - 'a'], cost[i]);\n    for (int k = 0; k < 26; ++k)\n      for (int i = 0; i < 26;++i)\n        for (int j = 0; j < 26; ++j)\n          if (d[i][k] + d[k][j] < d[i][j])\n            d[i][j] = d[i][k] + d[k][j];\n    long ans = 0;\n    for (int i = 0; i < s.size(); ++i) {\n      if (d[s[i] - 'a'][t[i] - 'a'] >= 1e9) return -1; \n      ans += d[s[i] - 'a'][t[i] - 'a'];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;strings&nbsp;source&nbsp;and&nbsp;target, both of length&nbsp;n&nbsp;and consisting of&nbsp;lowercase&nbsp;English letters. You are also given two&nbsp;0-indexed&nbsp;character arrays&nbsp;original&nbsp;and&nbsp;changed, and an integer array&nbsp;cost, where&nbsp;cost[i]&nbsp;represents the cost of changing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,177,87],"class_list":["post-10100","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-medium","tag-shortest-path","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10100"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10100\/revisions"}],"predecessor-version":[{"id":10102,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10100\/revisions\/10102"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}