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
s, excluding the
'*' between each pair of
Note that each
'|' will belong to exactly one pair.
Input: s = "l|*e*et|c**o|*de|"
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.
- split ‘|’ from input.
- s= [‘l’, ‘*e*et’, ‘c**o’, ‘*de’, ‘’]
- check 0, 2, 4, 6….string and count ‘*’ number.
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