{"id":9757,"date":"2022-06-25T13:38:13","date_gmt":"2022-06-25T20:38:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9757"},"modified":"2022-06-25T13:38:54","modified_gmt":"2022-06-25T20:38:54","slug":"leetcode-2315-count-asterisks","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2315-count-asterisks\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2315. Count Asterisks"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>, where every&nbsp;<strong>two<\/strong>&nbsp;consecutive vertical bars&nbsp;<code>'|'<\/code>&nbsp;are grouped into a&nbsp;<strong>pair<\/strong>. In other words, the 1<sup>st<\/sup>&nbsp;and 2<sup>nd<\/sup>&nbsp;<code>'|'<\/code>&nbsp;make a pair, the 3<sup>rd<\/sup>&nbsp;and 4<sup>th<\/sup>&nbsp;<code>'|'<\/code>&nbsp;make a pair, and so forth.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of&nbsp;<\/em><code>'*'<\/code><em>&nbsp;in&nbsp;<\/em><code>s<\/code><em>,&nbsp;<strong>excluding<\/strong>&nbsp;the&nbsp;<\/em><code>'*'<\/code><em>&nbsp;between each pair of&nbsp;<\/em><code>'|'<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that each&nbsp;<code>'|'<\/code>&nbsp;will belong to&nbsp;<strong>exactly<\/strong>&nbsp;one pair.<\/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 = \"l|*e*et|c**o|*de|\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The considered characters are underlined: \"l|*e*et|c**o|*de|\".\nThe characters between the first and second '|' are excluded from the answer.\nAlso, the characters between the third and fourth '|' are excluded from the answer.\nThere are 2 asterisks considered. Therefore, we return 2.<\/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 = \"iamprogrammer\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> In this example, there are no asterisks in s. Therefore, we return 0.\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 = \"yo|uar|e**|b|e***au|tifu|l\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The considered characters are underlined: \"yo|uar|e**|b|e***au|tifu|l\". There are 5 asterisks considered. Therefore, we return 5.<\/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;= 1000<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters, vertical bars&nbsp;<code>'|'<\/code>, and asterisks&nbsp;<code>'*'<\/code>.<\/li><li><code>s<\/code>&nbsp;contains an&nbsp;<strong>even<\/strong>&nbsp;number of vertical bars&nbsp;<code>'|'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>Count the number of bars so far, and only count &#8216;*&#8217; when there are even number of bars on the left.<\/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  int countAsterisks(string s) {\n    int bars = 0;\n    int ans = 0;    \n    for (char c : s) {\n      if (c == '*' && bars % 2 == 0)\n        ++ans;\n      if (c == '|') ++bars;      \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s, where every&nbsp;two&nbsp;consecutive vertical bars&nbsp;&#8216;|&#8217;&nbsp;are grouped into a&nbsp;pair. In other words, the 1st&nbsp;and 2nd&nbsp;&#8216;|&#8217;&nbsp;make a pair, the 3rd&nbsp;and 4th&nbsp;&#8216;|&#8217;&nbsp;make a pair,&#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-9757","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\/9757","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=9757"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9757\/revisions"}],"predecessor-version":[{"id":9759,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9757\/revisions\/9759"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}