DEV Community

Muthu
Muthu

Posted on

Find number of `bulb` occurrences from a given string

Question:
A string contains bulb string along with others, sometime, it is the continuous one like bulbulb. Find the number of occurrences of bulb.

Ex:
Given: "fbulbhdskjfhdskbulbdsfkjdshfdskbulbulbdsfsdfds"
Expected: 4

Solution:

count = 0
for i, c in enumerate(bulb_str):
  if c == "b" and (bulb_str[i:i+4]) == "bulb":
      count += 1
print(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

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️