欢迎光临散文网 会员登陆 & 注册

LeetCode 2315. Count Asterisks

2023-03-27 10:04 作者:您是打尖儿还是住店呢  | 我要投稿

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.

 

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 <= 1000

  • s consists of lowercase English letters, vertical bars '|', and asterisks '*'.

  • s contains an even number of vertical bars '|'.


Runtime: 1 ms, faster than 99.68% of Java online submissions for Count Asterisks.

Memory Usage: 40.8 MB, less than 26.44% of Java online submissions for Count Asterisks.


LeetCode 2315. Count Asterisks的评论 (共 条)

分享到微博请遵守国家法律