{"id":9395,"date":"2022-01-02T00:11:15","date_gmt":"2022-01-02T08:11:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9395"},"modified":"2022-01-02T00:11:37","modified_gmt":"2022-01-02T08:11:37","slug":"leetcode-2124-check-if-all-as-appears-before-all-bs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2124-check-if-all-as-appears-before-all-bs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2124. Check if All A&#8217;s Appears Before All B&#8217;s"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;consisting of&nbsp;<strong>only<\/strong>&nbsp;the characters&nbsp;<code>'a'<\/code>&nbsp;and&nbsp;<code>'b'<\/code>, return&nbsp;<code>true<\/code>&nbsp;<em>if&nbsp;<strong>every<\/strong>&nbsp;<\/em><code>'a'<\/code>&nbsp;<em>appears before&nbsp;<strong>every<\/strong>&nbsp;<\/em><code>'b'<\/code><em>&nbsp;in the string<\/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 = \"aaabbb\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nThe 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.\nHence, every 'a' appears before every 'b' and we 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 = \"abab\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nThere is an 'a' at index 2 and a 'b' at index 1.\nHence, not every 'a' appears before every 'b' and we 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 = \"bbb\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nThere are no 'a's, hence, every 'a' appears before every 'b' and we return true.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><code>1 &lt;= s.length &lt;= 100<\/code><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\"><code>s[i]<\/code>&nbsp;is either&nbsp;<code>'a'<\/code>&nbsp;or&nbsp;<code>'b'<\/code>.<br><br><strong>Solution: Count bs<\/strong><\/h2>\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 checkString(string s) {\n    int b = 0;\n    for (char ch : s) {\n      b += ch == 'b';\n      if (ch == 'a' && b) return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;consisting of&nbsp;only&nbsp;the characters&nbsp;&#8216;a&#8217;&nbsp;and&nbsp;&#8216;b&#8217;, return&nbsp;true&nbsp;if&nbsp;every&nbsp;&#8216;a&#8217;&nbsp;appears before&nbsp;every&nbsp;&#8216;b&#8217;&nbsp;in the string. Otherwise, return&nbsp;false. Example 1: Input: s = &#8220;aaabbb&#8221; Output: true Explanation: The &#8216;a&#8217;s are at indices&#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":[222,4],"class_list":["post-9395","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9395","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=9395"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9395\/revisions"}],"predecessor-version":[{"id":9397,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9395\/revisions\/9397"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}