[2315] Count Asterisks | Leetcode Easy

yoongyum·2022년 7월 1일
0

코딩테스트 🧩

목록 보기
42/47
post-thumbnail

🔎문제설명

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.



🧊파이썬 코드

class Solution:
    def countAsterisks(self, s: str) -> int:
        stack= False;
        answer = 0;
        for i in range(len(s)):
            if s[i] == '|':
                stack = not stack
            if s[i] == '*' and stack == False:
                answer+=1
        return answer

다른사람 풀이

#입력 : "l|*e*et|c**o|*de|"
class Solution:
    def countAsterisks(self, s: str) -> int:
        ls=s.split('|')[::2] #['l', 'c**o', '']  두개씩 끊어서 저장한다.
        ans=0
        for i in ls:
            ans+=i.count('*')
        return ans

0개의 댓글