{"id":8481,"date":"2021-08-05T21:13:23","date_gmt":"2021-08-06T04:13:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8481"},"modified":"2021-08-05T21:14:19","modified_gmt":"2021-08-06T04:14:19","slug":"leetcode-1869-longer-contiguous-segments-of-ones-than-zeros","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1869-longer-contiguous-segments-of-ones-than-zeros\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1869. Longer Contiguous Segments of Ones than Zeros"},"content":{"rendered":"\n<p>Given a binary string&nbsp;<code>s<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if the&nbsp;<strong>longest<\/strong>&nbsp;contiguous segment of&nbsp;<\/em><code>1<\/code><em>s is&nbsp;<strong>strictly longer<\/strong>&nbsp;than the&nbsp;<strong>longest<\/strong>&nbsp;contiguous segment of&nbsp;<\/em><code>0<\/code><em>s in&nbsp;<\/em><code>s<\/code>. Return&nbsp;<code>false<\/code><em>&nbsp;otherwise<\/em>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, in&nbsp;<code>s = \"<u>11<\/u>01<u>000<\/u>10\"<\/code>&nbsp;the longest contiguous segment of&nbsp;<code>1<\/code>s has length&nbsp;<code>2<\/code>, and the longest contiguous segment of&nbsp;<code>0<\/code>s has length&nbsp;<code>3<\/code>.<\/li><\/ul>\n\n\n\n<p>Note that if there are no&nbsp;<code>0<\/code>s, then the longest contiguous segment of&nbsp;<code>0<\/code>s is considered to have length&nbsp;<code>0<\/code>. The same applies if there are no&nbsp;<code>1<\/code>s.<\/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 = \"1101\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nThe longest contiguous segment of 1s has length 2: \"1101\"\nThe longest contiguous segment of 0s has length 1: \"1101\"\nThe segment of 1s is longer, so return true.\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 = \"111000\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nThe longest contiguous segment of 1s has length 3: \"111000\"\nThe longest contiguous segment of 0s has length 3: \"111000\"\nThe segment of 1s is not longer, so return false.\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 = \"110100010\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nThe longest contiguous segment of 1s has length 2: \"110100010\"\nThe longest contiguous segment of 0s has length 3: \"110100010\"\nThe segment of 1s is not longer, so return false.\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;= 100<\/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: Brute Force<\/strong><\/h2>\n\n\n\n<p>Write a function <span style=\"text-decoration: underline;\"><em><strong>count<\/strong><\/em> <\/span>to count longest contiguous segment of m, return count(&#8216;1&#8217;) &gt; count(&#8216;0&#8217;)<\/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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkZeroOnes(string s) {\n    auto count = [&](char m) {\n      int ans = 0;\n      int l = 0;\n      for (char c : s)\n        if (c != m) l = 0;\n        else ans = max(ans, ++l);      \n      return ans;\n    };\n    return count('1') > count('0');\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s, return&nbsp;true&nbsp;if the&nbsp;longest&nbsp;contiguous segment of&nbsp;1s is&nbsp;strictly longer&nbsp;than the&nbsp;longest&nbsp;contiguous segment of&nbsp;0s in&nbsp;s. Return&nbsp;false&nbsp;otherwise. For example, in&nbsp;s = &#8220;110100010&#8221;&nbsp;the longest contiguous segment of&nbsp;1s has&#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":[713,222,4],"class_list":["post-8481","post","type-post","status-publish","format-standard","hentry","category-string","tag-contiguous","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8481","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=8481"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8481\/revisions"}],"predecessor-version":[{"id":8483,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8481\/revisions\/8483"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}