{"id":9509,"date":"2022-02-26T21:18:20","date_gmt":"2022-02-27T05:18:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9509"},"modified":"2022-02-26T21:20:44","modified_gmt":"2022-02-27T05:20:44","slug":"leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2186-minimum-number-of-steps-to-make-two-strings-anagram-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2186. Minimum Number of Steps to Make Two Strings Anagram II"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>. In one step, you can append&nbsp;<strong>any character<\/strong>&nbsp;to either&nbsp;<code>s<\/code>&nbsp;or&nbsp;<code>t<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of steps to make&nbsp;<\/em><code>s<\/code><em>&nbsp;and&nbsp;<\/em><code>t<\/code><em>&nbsp;<strong>anagrams<\/strong>&nbsp;of each other.<\/em><\/p>\n\n\n\n<p>An&nbsp;<strong>anagram<\/strong>&nbsp;of a string is a string that contains the same characters with a different (or the same) ordering.<\/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> s = \"<strong><u>lee<\/u><\/strong>tco<strong>de<\/strong>\", t = \"co<strong>a<\/strong>t<strong>s<\/strong>\"\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> \n- In 2 steps, we can append the letters in \"as\" onto s = \"leetcode\", forming s = \"leetcode<strong><u>as<\/u><\/strong>\".\n- In 5 steps, we can append the letters in \"leede\" onto t = \"coats\", forming t = \"coats<strong>leede<\/strong>\".\n\"leetcodeas\" and \"coatsleede\" are now anagrams of each other.\nWe used a total of 2 + 5 = 7 steps.\nIt can be shown that there is no way to make them anagrams of each other with less than 7 steps.\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> s = \"night\", t = \"thing\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The given strings are already anagrams of each other. Thus, we do not need any further steps.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length, t.length &lt;= 2 * 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Record the frequency difference of each letter.<\/p>\n\n\n\n<p>Ans = sum(diff)<\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(26)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minSteps(string s, string t) {\n    vector<int> diff(26);\n    for (char c : s) ++diff[c - 'a'];\n    for (char c : t) --diff[c - 'a'];\n    int ans = 0;\n    for (int d : diff)\n      ans += abs(d);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;s&nbsp;and&nbsp;t. In one step, you can append&nbsp;any character&nbsp;to either&nbsp;s&nbsp;or&nbsp;t. Return&nbsp;the minimum number of steps to make&nbsp;s&nbsp;and&nbsp;t&nbsp;anagrams&nbsp;of each other. An&nbsp;anagram&nbsp;of a string&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[304,82,4],"class_list":["post-9509","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-anagram","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9509","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=9509"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9509\/revisions"}],"predecessor-version":[{"id":9511,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9509\/revisions\/9511"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}