{"id":8102,"date":"2021-02-13T20:34:31","date_gmt":"2021-02-14T04:34:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8102"},"modified":"2021-02-13T20:45:51","modified_gmt":"2021-02-14T04:45:51","slug":"leetcode-1758-minimum-changes-to-make-alternating-binary-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1758-minimum-changes-to-make-alternating-binary-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1758. Minimum Changes To Make Alternating Binary String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;consisting only of the characters&nbsp;<code>'0'<\/code>&nbsp;and&nbsp;<code>'1'<\/code>. In one operation, you can change any&nbsp;<code>'0'<\/code>&nbsp;to&nbsp;<code>'1'<\/code>&nbsp;or vice versa.<\/p>\n\n\n\n<p>The string is called alternating if no two adjacent characters are equal. For example, the string&nbsp;<code>\"010\"<\/code>&nbsp;is alternating, while the string&nbsp;<code>\"0100\"<\/code>&nbsp;is not.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of operations needed to make<\/em>&nbsp;<code>s<\/code>&nbsp;<em>alternating<\/em>.<\/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 = \"0100\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> If you change the last character to '1', s will be \"0101\", which is 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 = \"10\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> s is already alternating.\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 = \"1111\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You need two operations to reach \"0101\" or \"1010\".\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;= 10<sup>4<\/sup><\/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: Two Counters<\/strong><\/h2>\n\n\n\n<p>The final string is either 010101&#8230; or 101010&#8230;<br>We just need two counters to record the number of changes needed to transform the original string to those two final strings.<\/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 minOperations(string s) {\n    int c1 = 0, c2 = 0;\n    for (int i = 0; i < s.length(); ++i) {\n      c1 += (s[i] - '0' == i % 2);\n      c2 += (s[i] - '0' != i % 2);\n    }\n    return min(c1, c2);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;consisting only of the characters&nbsp;&#8216;0&#8217;&nbsp;and&nbsp;&#8216;1&#8217;. In one operation, you can change any&nbsp;&#8216;0&#8217;&nbsp;to&nbsp;&#8216;1&#8217;&nbsp;or vice versa. The string is called alternating if no&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[8,222,4],"class_list":["post-8102","post","type-post","status-publish","format-standard","hentry","category-string","tag-counting","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8102","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=8102"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8102\/revisions"}],"predecessor-version":[{"id":8104,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8102\/revisions\/8104"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}