Skip to content

Commit c12a775

Browse files
authored
Create count-asterisks.cpp
1 parent c462db3 commit c12a775

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

C++/count-asterisks.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// string
5+
class Solution {
6+
public:
7+
int countAsterisks(string s) {
8+
int result = 0, cnt = 0;
9+
for (const auto& c : s) {
10+
if (c == '|') {
11+
cnt = (cnt + 1) % 2;
12+
continue;
13+
}
14+
if (c == '*' && cnt == 0) {
15+
++result;
16+
}
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)