Find substrings that contain all vowels in Python

PythonServer Side ProgrammingProgramming

Suppose we have a string in lowercase alphabets, we have to find substrings that contain all the vowels at least once and there exist no consonants in that substrings.

So, if the input is like "helloworldaeiouaieuonicestring", then the output will be ['aeiou', 'aeioua', 'aeiouai', 'aeiouaiu', 'eioua', 'eiouai', 'eiouaiu']

To solve this, we will follow these steps −

  • n := size of s

  • for i in range 0 to n, do

    • my_map := a new map

    • for j in range i to n, do

      • if s[j] not vowel, then

        • come out from the loop

      • my_map[s[j]] := 1

      • if size of my_map is same as 5, then

        • display s[from index i to j + 1]

Example 

Let us see the following implementation to get better understanding −

 Live Demo

def isVowel(x):
   if x in ['a','e','i','o','u']:
      return True
   return False
def get_substrings(s):
   n = len(s)
   for i in range(n):
      my_map = dict()
      for j in range(i, n):
         if (isVowel(s[j]) == False):
            break
         my_map[s[j]] = 1
         if (len(my_map) == 5):
            print(s[i:j + 1])
s = "helloworldaeiouaiunicestring"
get_substrings(s)

Input

"helloworldaeiouaiunicestring"

Output

aeiou
aeioua
aeiouai
aeiouaiu
eioua
eiouai
eiouaiu
raja
Published on 19-Aug-2020 14:57:47
Advertisements