[LeetCode]#2315. Count Asterisks
1 min readNov 24, 2022
Environment: Python 3.8
Key technique: split
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.
Analysis:
- split ‘|’ from input.
- s= [‘l’, ‘*e*et’, ‘c**o’, ‘*de’, ‘’]
- check 0, 2, 4, 6….string and count ‘*’ number.
Solution:
class Solution:
def countAsterisks(self, s: str) -> int:
words = s.split('|')
ans = 0
for i in range(0, len(words), 2):
for j in words[i]:
if j == '*':
ans += 1
return ans
Submissions:
Reference:
https://leetcode.com/problems/count-asterisks/discuss/2545886/Python-Solution