|
| 1 | +# [2315. Count Asterisks](https://leetcode.com/problems/count-asterisks) |
| 2 | + |
| 3 | +[中文文档](/solution/2300-2399/2315.Count%20Asterisks/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a string <code>s</code>, where every <strong>two</strong> consecutive vertical bars <code>'|'</code> are grouped into a <strong>pair</strong>. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> <code>'|'</code> make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> <code>'|'</code> make a pair, and so forth.</p> |
| 8 | + |
| 9 | +<p>Return <em>the number of </em><code>'*'</code><em> in </em><code>s</code><em>, <strong>excluding</strong> the </em><code>'*'</code><em> between each pair of </em><code>'|'</code>.</p> |
| 10 | + |
| 11 | +<p><strong>Note</strong> that each <code>'|'</code> will belong to <strong>exactly</strong> one pair.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong>Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> s = "l|*e*et|c**o|*de|" |
| 18 | +<strong>Output:</strong> 2 |
| 19 | +<strong>Explanation:</strong> The considered characters are underlined: "<u>l</u>|*e*et|<u>c**o</u>|*de|". |
| 20 | +The characters between the first and second '|' are excluded from the answer. |
| 21 | +Also, the characters between the third and fourth '|' are excluded from the answer. |
| 22 | +There are 2 asterisks considered. Therefore, we return 2.</pre> |
| 23 | + |
| 24 | +<p><strong>Example 2:</strong></p> |
| 25 | + |
| 26 | +<pre> |
| 27 | +<strong>Input:</strong> s = "iamprogrammer" |
| 28 | +<strong>Output:</strong> 0 |
| 29 | +<strong>Explanation:</strong> In this example, there are no asterisks in s. Therefore, we return 0. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p><strong>Example 3:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> s = "yo|uar|e**|b|e***au|tifu|l" |
| 36 | +<strong>Output:</strong> 5 |
| 37 | +<strong>Explanation:</strong> The considered characters are underlined: "<u>yo</u>|uar|<u>e**</u>|b|<u>e***au</u>|tifu|<u>l</u>". There are 5 asterisks considered. Therefore, we return 5.</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= s.length <= 1000</code></li> |
| 44 | + <li><code>s</code> consists of lowercase English letters, vertical bars <code>'|'</code>, and asterisks <code>'*'</code>.</li> |
| 45 | + <li><code>s</code> contains an <strong>even</strong> number of vertical bars <code>'|'</code>.</li> |
| 46 | +</ul> |
| 47 | + |
| 48 | + |
| 49 | +## Solutions |
| 50 | + |
| 51 | +<!-- tabs:start --> |
| 52 | + |
| 53 | +### **Python3** |
| 54 | + |
| 55 | +```python |
| 56 | +class Solution: |
| 57 | + def countAsterisks(self, s: str) -> int: |
| 58 | + ans = t = 0 |
| 59 | + for c in s: |
| 60 | + if c == '|': |
| 61 | + t ^= 1 |
| 62 | + elif c == '*': |
| 63 | + if t == 0: |
| 64 | + ans += 1 |
| 65 | + return ans |
| 66 | +``` |
| 67 | + |
| 68 | +### **Java** |
| 69 | + |
| 70 | +```java |
| 71 | +class Solution { |
| 72 | + public int countAsterisks(String s) { |
| 73 | + int ans = 0, t = 0; |
| 74 | + for (char c : s.toCharArray()) { |
| 75 | + if (c == '|') { |
| 76 | + t ^= 1; |
| 77 | + } else if (c == '*') { |
| 78 | + if (t == 0) { |
| 79 | + ++ans; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return ans; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### **C++** |
| 89 | + |
| 90 | +```cpp |
| 91 | +class Solution { |
| 92 | +public: |
| 93 | + int countAsterisks(string s) { |
| 94 | + int ans = 0, t = 0; |
| 95 | + for (char& c : s) |
| 96 | + { |
| 97 | + if (c == '|') t ^= 1; |
| 98 | + else if (c == '*') ans += t == 0; |
| 99 | + } |
| 100 | + return ans; |
| 101 | + } |
| 102 | +}; |
| 103 | +``` |
| 104 | +
|
| 105 | +### **Go** |
| 106 | +
|
| 107 | +```go |
| 108 | +func countAsterisks(s string) int { |
| 109 | + ans, t := 0, 0 |
| 110 | + for _, c := range s { |
| 111 | + if c == '|' { |
| 112 | + t ^= 1 |
| 113 | + } else if c == '*' { |
| 114 | + if t == 0 { |
| 115 | + ans++ |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return ans |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### **TypeScript** |
| 124 | + |
| 125 | +```ts |
| 126 | + |
| 127 | +``` |
| 128 | + |
| 129 | +### **...** |
| 130 | + |
| 131 | +``` |
| 132 | +
|
| 133 | +``` |
| 134 | + |
| 135 | +<!-- tabs:end --> |
0 commit comments