{"id":7849,"date":"2020-12-26T18:47:25","date_gmt":"2020-12-27T02:47:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7849"},"modified":"2020-12-26T18:47:49","modified_gmt":"2020-12-27T02:47:49","slug":"leetcode-1702-maximum-binary-string-after-change","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1702-maximum-binary-string-after-change\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1702. Maximum Binary String After Change"},"content":{"rendered":"\n<p>You are given a binary string&nbsp;<code>binary<\/code>&nbsp;consisting of only&nbsp;<code>0<\/code>&#8216;s or&nbsp;<code>1<\/code>&#8216;s. You can apply each of the following operations any number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Operation 1: If the number contains the substring&nbsp;<code>\"00\"<\/code>, you can replace it with&nbsp;<code>\"10\"<\/code>.<ul><li>For example,&nbsp;<code>\"00010\" -&gt; \"10010<\/code>&#8220;<\/li><\/ul><\/li><li>Operation 2: If the number contains the substring&nbsp;<code>\"10\"<\/code>, you can replace it with&nbsp;<code>\"01\"<\/code>.<ul><li>For example,&nbsp;<code>\"00010\" -&gt; \"00001\"<\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<p><em>Return the&nbsp;<strong>maximum binary string<\/strong>&nbsp;you can obtain after any number of operations. Binary string&nbsp;<code>x<\/code>&nbsp;is greater than binary string&nbsp;<code>y<\/code>&nbsp;if&nbsp;<code>x<\/code>&#8216;s decimal representation is greater than&nbsp;<code>y<\/code>&#8216;s decimal representation.<\/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> binary = \"000110\"\n<strong>Output:<\/strong> \"111011\"\n<strong>Explanation:<\/strong> A valid transformation sequence can be:\n\"000110\" -&gt; \"000101\" \n\"000101\" -&gt; \"100101\" \n\"100101\" -&gt; \"110101\" \n\"110101\" -&gt; \"110011\" \n\"110011\" -&gt; \"111011\"\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> binary = \"01\"\n<strong>Output:<\/strong> \"01\"\n<strong>Explanation:<\/strong>&nbsp;\"01\" cannot be transformed any further.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= binary.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>binary<\/code>&nbsp;consist of&nbsp;<code>'0'<\/code>&nbsp;and&nbsp;<code>'1'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Counting<\/strong><\/h2>\n\n\n\n<p>Leading 1s are good, no need to change them.<br>For the rest of the string<br>1. Apply operation 2 to make the string into 3 parts, leading 1s, middle 0s and tailing 1s.<br>e.g. 110<strong>1010<\/strong>1 =&gt; 11001<strong>10<\/strong>1 =&gt; 1100<strong>10<\/strong>11 =&gt; 11000111<br>2. Apply operation 1 to make flip zeros to ones except the last one.<br>e.g. 11<strong>00<\/strong>0111 =&gt; 111<strong>00<\/strong>111 =&gt; 11110111<br><br>There will be only one zero (if the input string is not all 1s) is the final largest string, the position of the zero is leading 1s + zeros &#8211; 1.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  string maximumBinaryString(string s) {\n    const int n = s.length();\n    int l = 0;\n    int z = 0;\n    for (char& c : s) {\n      if (c == '0') {\n        ++z;\n      } else if (z == 0) { \/\/ leading 1s      \n        ++l;\n      }\n      c = '1';\n    }\n    if (l != n) s[l + z - 1] = '0';\n    return s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a binary string&nbsp;binary&nbsp;consisting of only&nbsp;0&#8216;s or&nbsp;1&#8216;s. You can apply each of the following operations any number of times: Operation 1: If the&#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":[39,8,88,177,4],"class_list":["post-7849","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-binary","tag-counting","tag-greedy","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7849","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=7849"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7849\/revisions"}],"predecessor-version":[{"id":7851,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7849\/revisions\/7851"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}