{"id":8467,"date":"2021-08-04T22:29:11","date_gmt":"2021-08-05T05:29:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8467"},"modified":"2021-08-04T22:30:31","modified_gmt":"2021-08-05T05:30:31","slug":"leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1864-minimum-number-of-swaps-to-make-the-binary-string-alternating\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1864. Minimum Number of Swaps to Make the Binary String Alternating"},"content":{"rendered":"\n<p>Given a binary string&nbsp;<code>s<\/code>, return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of character swaps to make it&nbsp;<strong>alternating<\/strong>, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it is impossible.<\/em><\/p>\n\n\n\n<p>The string is called&nbsp;<strong>alternating<\/strong>&nbsp;if no two adjacent characters are equal. For example, the strings&nbsp;<code>\"010\"<\/code>&nbsp;and&nbsp;<code>\"1010\"<\/code>&nbsp;are alternating, while the string&nbsp;<code>\"0100\"<\/code>&nbsp;is not.<\/p>\n\n\n\n<p>Any two characters may be swapped, even if they are&nbsp;<strong>not adjacent<\/strong>.<\/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 = \"111000\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Swap positions 1 and 4: \"111000\" -&gt; \"101010\"\nThe string is now alternating.\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 = \"010\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The string is already alternating, no swaps are needed.\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> s = \"1110\"\n<strong>Output:<\/strong> -1\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 &lt;= 1000<\/code><\/li><li><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Two passes, make the string starts with &#8216;0&#8217; or &#8216;1&#8217;, count how many 0\/1 swaps needed. 0\/1 swaps must equal otherwise it&#8217;s impossible to swap.<\/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 minSwaps(string s) {    \n    auto count = [&](int m) {\n      vector<int> swaps(2);\n      for (int i = 0; i < s.length(); ++i, m ^= 1)\n        if (s[i] - '0' != m)\n          ++swaps[s[i] - '0'];\n      return swaps[0] == swaps[1] ? swaps[0] : INT_MAX;\n    };\n    \n    int ans = min(count(0), count(1));\n    return ans != INT_MAX ? ans : -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s, return&nbsp;the&nbsp;minimum&nbsp;number of character swaps to make it&nbsp;alternating, or&nbsp;-1&nbsp;if it is impossible. The string is called&nbsp;alternating&nbsp;if no two adjacent characters are equal.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[712,88,4,290],"class_list":["post-8467","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-alternating","tag-greedy","tag-string","tag-swap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8467","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=8467"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8467\/revisions"}],"predecessor-version":[{"id":8470,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8467\/revisions\/8470"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}