Leetcode Solutions: Count Asterisks

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 eac…


This content originally appeared on DEV Community and was authored by SalahElhossiny

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.

class Solution(object):
    def countAsterisks(self, s):

        if not "*" in s:
            return 0

        stack = []
        count = 0

        for char in s:
            if char == "|":
                count += 1

            stack.append(char)

            if count == 2:
                while count:
                    el = stack.pop()
                    if el == "|":
                        count -= 1


        return "".join(stack).count("*") 





This content originally appeared on DEV Community and was authored by SalahElhossiny


Print Share Comment Cite Upload Translate Updates
APA

SalahElhossiny | Sciencx (2022-07-02T20:08:22+00:00) Leetcode Solutions: Count Asterisks. Retrieved from https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/

MLA
" » Leetcode Solutions: Count Asterisks." SalahElhossiny | Sciencx - Saturday July 2, 2022, https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/
HARVARD
SalahElhossiny | Sciencx Saturday July 2, 2022 » Leetcode Solutions: Count Asterisks., viewed ,<https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/>
VANCOUVER
SalahElhossiny | Sciencx - » Leetcode Solutions: Count Asterisks. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/
CHICAGO
" » Leetcode Solutions: Count Asterisks." SalahElhossiny | Sciencx - Accessed . https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/
IEEE
" » Leetcode Solutions: Count Asterisks." SalahElhossiny | Sciencx [Online]. Available: https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/. [Accessed: ]
rf:citation
» Leetcode Solutions: Count Asterisks | SalahElhossiny | Sciencx | https://www.scien.cx/2022/07/02/leetcode-solutions-count-asterisks/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.