2315. 统计星号
2315. 统计星号
题目
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'*'ins _,excluding the _'*'between each pair of'|'.
Note that each '|' will belong to exactly one pair.
Example 1:
Input: s =
"l|*e*et|c**o|*de|"Output: 2
Explanation: The considered characters are underlined:
"l|*e*et|c**o|*de|".The characters between the first and second '|' are excluded from the answer.
Also, the characters between the third and fourth '|' are excluded from the answer.
There are 2 asterisks considered. Therefore, we return 2.
Example 2:
Input: s = "iamprogrammer"
Output: 0
Explanation: In this example, there are no asterisks in s. Therefore, we return 0.
Example 3:
Input: s =
"yo|uar|e**|b|e***au|tifu|l"Output: 5
Explanation: The considered characters are underlined:
"yo|uar|e**|b|e***au|tifu|l". There are 5 asterisks considered. Therefore, we return 5.
Constraints:
1 <= s.length <= 1000sconsists of lowercase English letters, vertical bars'|', and asterisks'*'.scontains an even number of vertical bars'|'.
题目大意
给你一个字符串 s ,每 两个 连续竖线 '|' 为 一对 。换言之,第一个和第二个 '|' 为一对,第三个和第四个 '|' 为一对,以此类推。
请你返回 不在 竖线对之间,s 中 '*' 的数目。
注意 ,每个竖线 '|' 都会 恰好 属于一个对。
示例 1:
输入: s =
"l|*e*et|c**o|*de|"输出: 2
解释: 不在竖线对之间的字符加粗加斜体后,得到字符串:
"l|*e*et|c**o|*de|"。第一和第二条竖线 '|' 之间的字符不计入答案。
同时,第三条和第四条竖线 '|' 之间的字符也不计入答案。
不在竖线对之间总共有 2 个星号,所以我们返回 2 。
示例 2:
输入: s = "iamprogrammer"
输出: 0
解释: 在这个例子中,s 中没有星号。所以返回 0 。
示例 3:
输入: s =
"yo|uar|e**|b|e***au|tifu|l"输出: 5
解释: 需要考虑的字符加粗加斜体后:
"yo|uar|e**|b|e***au|tifu|l"。不在竖线对之间总共有 5 个星号。所以我们返回 5 。
提示:
1 <= s.length <= 1000s只包含小写英文字母,竖线'|'和星号'*'。s包含 偶数 个竖线'|'。
解题思路
初始化变量:
count:统计星号*的数量。bars:记录竖线|的出现次数,用于判断当前星号是否在竖线包围内。
遍历字符串:
- 遍历字符串中的每个字符。
- 如果遇到竖线
|,增加bars的计数。 - 如果遇到星号
*,检查bars的奇偶性:- 如果
bars是偶数,说明当前星号不在竖线包围内,增加count。
- 如果
返回结果:
- 遍历完成后,返回统计的星号数量
count。
- 遍历完成后,返回统计的星号数量
复杂度分析
- 时间复杂度:
O(n),其中n是字符串的长度,遍历字符串。 - 空间复杂度:
O(1),只使用了常数级变量。
代码
/**
* @param {string} s
* @return {number}
*/
var countAsterisks = function (s) {
let count = 0,
bars = 0;
for (let char of s) {
if (char == '|') {
bars++;
}
if (char == '*' && bars % 2 == 0) {
count++;
}
}
return count;
};
