- Related Questions & Answers
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Find all distinct palindromic sub-strings of a given String in Python
- Java program to count the characters in each word in a given sentence
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Program to find the length of the longest, that can be made using given letters in Python
- Find uncommon characters of the two strings in C++
- C++ program to find uncommon characters in two given strings
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Find largest word in dictionary by deleting some characters of given string
- Program to find number of tasks can be finished with given conditions in Python
- Program to count how many times we can find "pizza" with given string characters in Python
- Print elements that can be added to form a given sum
- Count number of distinct pairs whose sum exists in the given array in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string S (all letters are in lowercase), we have to find the count of all of the sub-strings of length four whose characters can be rearranged to form this word "bird".

So, if the input is like "birdb", then the output will be 2.

To solve this, we will follow these steps −

cnt := 0

for i in range 0 to size of s - 3, do

bird := an array with [0, 0, 0, 0]

for j in range i to i + 4, do

if s[j] is same as 'b', then

bird[0] := bird[0] + 1

otherwise when s[j] is same as 'i', then

bird[1] := bird[1] + 1

otherwise when s[j] is same as 'r', then

bird[2] := bird[2] + 1

otherwise when s[j] is same as 'd', then

bird[3] := bird[3] + 1

if bird is same as [1,1,1,1], then

cnt := cnt + 1

return cnt

Let us see the following implementation to get better understanding −

def number_of_occurrence(s): cnt = 0 for i in range(0, len(s) - 3): bird = [0, 0, 0, 0] for j in range(i, i + 4): if s[j] == 'b': bird[0] += 1 elif s[j] == 'i': bird[1] += 1 elif s[j] == 'r': bird[2] += 1 elif s[j] == 'd': bird[3] += 1 if bird == [1,1,1,1]: cnt += 1 return cnt s = "birdb" print(number_of_occurrence(s))

"birdb"

2

Advertisements