{"id":8209,"date":"2021-03-06T22:24:55","date_gmt":"2021-03-07T06:24:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8209"},"modified":"2021-03-06T22:25:17","modified_gmt":"2021-03-07T06:25:17","slug":"leetcode-1784-check-if-binary-string-has-at-most-one-segment-of-ones","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1784-check-if-binary-string-has-at-most-one-segment-of-ones\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1784. Check if Binary String Has at Most One Segment of Ones"},"content":{"rendered":"\n<p>Given a binary string&nbsp;<code>s<\/code>&nbsp;<strong>\u200b\u200b\u200b\u200b\u200bwithout leading zeros<\/strong>, return&nbsp;<code>true<\/code>\u200b\u200b\u200b&nbsp;<em>if&nbsp;<\/em><code>s<\/code><em>&nbsp;contains&nbsp;<strong>at most one contiguous segment of ones<\/strong><\/em>. Otherwise, return&nbsp;<code>false<\/code>.<\/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 = \"1001\"\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>The ones do not form a contiguous segment.\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 = \"110\"\n<strong>Output:<\/strong> true<\/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>\u200b\u200b\u200b\u200b is either&nbsp;<code>'0'<\/code>&nbsp;or&nbsp;<code>'1'<\/code>.<\/li><li><code>s[0]<\/code>&nbsp;is&nbsp;<code>'1'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>increase counter if s[i] == &#8216;1&#8217; otherwise, reset counter.<br>increase counts when counter becomes 1.<br>return counts == 1<\/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 checkOnesSegment(string s) {\n    int count = 0;\n    int ones = 0;\n    for (const char c : s) {\n      if (c == '1') {\n        count += (++ones == 1);        \n      } else {\n        ones = 0;\n      }\n    }\n    return count == 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s&nbsp;\u200b\u200b\u200b\u200b\u200bwithout leading zeros, return&nbsp;true\u200b\u200b\u200b&nbsp;if&nbsp;s&nbsp;contains&nbsp;at most one contiguous segment of ones. Otherwise, return&nbsp;false. Example 1: Input: s = &#8220;1001&#8221; Output: false Explanation: 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":[47],"tags":[4],"class_list":["post-8209","post","type-post","status-publish","format-standard","hentry","category-string","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8209","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=8209"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8209\/revisions"}],"predecessor-version":[{"id":8211,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8209\/revisions\/8211"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}