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)