2315. Count Asterisks

Question

You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.

Return *the number of '*' in s, excluding the '*' between each pair of *'|'.

Note that each '|' will belong to exactly one pair.

Solution

统计“|”字符出现的数量,如果数量为偶数时,则计算出现的“*”符号。

Code

1
2
3
4
5
6
7
8
9
10
class Solution {
public int countAsterisks(String s) {
int num = 0, count = 0;
for(char c : s.toCharArray()){
if(c == '|') num++;
else if((num & 1) == 0 && c == '*') count++;
}
return count;
}
}
Author

Xander

Posted on

2022-06-26

Updated on

2022-07-12

Licensed under

Comments