DEV Community

SalahElhossiny
SalahElhossiny

Posted on

2 2

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 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("*") 




Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay