{"id":5801,"date":"2019-11-07T08:50:05","date_gmt":"2019-11-07T16:50:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5801"},"modified":"2019-11-08T08:25:38","modified_gmt":"2019-11-08T16:25:38","slug":"leetcode-1247-minimum-swaps-to-make-strings-equal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1247-minimum-swaps-to-make-strings-equal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1247. Minimum Swaps to Make Strings Equal"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;of equal length&nbsp;consisting of letters&nbsp;<code>\"x\"<\/code>&nbsp;and&nbsp;<code>\"y\"<\/code>&nbsp;<strong>only<\/strong>. Your task is to&nbsp;make these two strings equal to each other. You can swap any two characters that belong to&nbsp;<strong>different<\/strong>&nbsp;strings,&nbsp;which means: swap&nbsp;<code>s1[i]<\/code>&nbsp;and&nbsp;<code>s2[j]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;the minimum number of swaps required&nbsp;to make&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;equal, or return&nbsp;<code>-1<\/code>&nbsp;if it is&nbsp;impossible to do so.<\/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> s1 = \"xx\", s2 = \"yy\"\n<strong>Output:<\/strong> 1\n<strong>Explanation: \n<\/strong>Swap s1[0] and s2[1], s1 = \"yx\", s2 = \"yx\".<\/pre>\n\n\n\n<p><strong>Example 2:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"xy\", s2 = \"yx\"\n<strong>Output:<\/strong> 2\n<strong>Explanation: \n<\/strong>Swap s1[0] and s2[0], s1 = \"yy\", s2 = \"xx\".\nSwap s1[0] and s2[1], s1 = \"xy\", s2 = \"xy\".\nNote that you can't swap s1[0] and s1[1] to make s1 equal to \"yx\", cause we can only swap chars in different strings.<\/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> s1 = \"xx\", s2 = \"xy\"\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"xxyyxyxyxx\", s2 = \"xyyxyxxxyx\"\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s1.length, s2.length &lt;= 1000<\/code><\/li><li><code>s1, s2<\/code>&nbsp;only contain&nbsp;<code>'x'<\/code>&nbsp;or&nbsp;<code>'y'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>if s1[i] == s2[i] than no need to swap, so we can only look for<br>case1. s1[i] = x, s2[i] = y, xy <br>case2. s1[i] = y, s2[i] = x, yx<br><br>If case1 + case2 is odd, then there&#8217;s no solution.<br><br>Otherwise we can use one swap to fix two xys (or two yxs)<br>xx, yy => xy, yx<br><br>One special case is there an extra xy and and extra yx, which takes two swaps<br>xy, yx => yy, xx => xy, xy<\/p>\n\n\n\n<p>Finally,<br>ans = (case1 + 1) \/ 2 + (case2 + 1) \/ 2<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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 minimumSwap(string s1, string s2) {        \n    int xy = 0;\n    int yx = 0;    \n    for (int i = 0; i < s1.length(); ++i) {\n      if (s1[i] == 'x' &#038;&#038; s2[i] == 'y') ++xy;\n      if (s1[i] == 'y' &#038;&#038; s2[i] == 'x') ++yx;\n    }\n    if ((xy + yx) % 2) return -1;\n    return (xy + 1) \/ 2 + (yx + 1) \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;s1&nbsp;and&nbsp;s2&nbsp;of equal length&nbsp;consisting of letters&nbsp;&#8220;x&#8221;&nbsp;and&nbsp;&#8220;y&#8221;&nbsp;only. Your task is to&nbsp;make these two strings equal to each other. You can swap any two&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,376],"class_list":["post-5801","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5801","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=5801"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5801\/revisions"}],"predecessor-version":[{"id":5804,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5801\/revisions\/5804"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}