Find substrings that contain all vowels
Last Updated :
06 Apr, 2023
We have been given a string in lowercase alphabets. We need to print substrings that contain all the vowels at least one time and there are no consonants (non-vowel characters) present in the substrings.
Examples:
Input : str = aeoibddaeoiud
Output : aeoiu
Input : str = aeoibsddaeiouudb
Output : aeiou, aeiouu
Reference:- Samsung Interview Questions.
We use a hashing based technique and start traversing the string from the start. For every character, we consider all substrings starting from it. If we encounter a consonant, we move to the next starting character. Else, we insert the current character in a hash. If all vowels are included, we print the current substring.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' );
}
void FindSubstring(string str)
{
set< char > hash;
int n = str.length();
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
if (isVowel(str[j]) == false )
break ;
hash.insert(str[j]);
if (hash.size() == 5)
cout << str.substr(i, j-i+1) << " " ;
}
hash.clear();
}
}
int main()
{
string str = "aeoibsddaeiouudb" ;
FindSubstring(str);
return 0;
}
|
Java
import java.util.HashSet;
public class GFG {
static boolean isVowel( char x) {
return (x == 'a' || x == 'e' || x == 'i'
|| x == 'o' || x == 'u' );
}
static void findSubstring(String str) {
HashSet<Character> hash = new HashSet<Character>();
int n = str.length();
for ( int i = 0 ; i < n; i++) {
for ( int j = i; j < n; j++) {
if (isVowel(str.charAt(j)) == false )
break ;
hash.add(str.charAt(j));
if (hash.size() == 5 )
System.out.print(str.substring(i, j + 1 ) + " " );
}
hash.clear();
}
}
public static void main(String[] args) {
String str = "aeoibsddaeiouudb" ;
findSubstring(str);
}
}
|
Python3
def isVowel(x):
if (x = = 'a' or x = = 'e' or x = = 'i' or
x = = 'o' or x = = 'u' ):
return True
return False
def FindSubstring(str1):
n = len (str1)
for i in range (n):
hash = dict ()
for j in range (i, n):
if (isVowel(str1[j]) = = False ):
break
hash [str1[j]] = 1
if ( len ( hash ) = = 5 ):
print (str1[i:j + 1 ], end = " " )
str1 = "aeoibsddaeiouudb"
FindSubstring(str1)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static bool isVowel( char x)
{
return (x == 'a' || x == 'e' ||
x == 'i' || x == 'o' || x == 'u' );
}
public static void findSubstring( string str)
{
HashSet< char > hash = new HashSet< char >();
int n = str.Length;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
if (isVowel(str[j]) == false )
{
break ;
}
hash.Add(str[j]);
if (hash.Count == 5)
{
Console.Write(str.Substring(i,
(j + 1) - i) + " " );
}
}
hash.Clear();
}
}
public static void Main( string [] args)
{
string str = "aeoibsddaeiouudb" ;
findSubstring(str);
}
}
|
Javascript
<script>
function isVowel(x)
{
return (x == 'a' || x == 'e' || x == 'i'
|| x == 'o' || x == 'u' );
}
function findSubstring(str)
{
let hash = new Set();
let n = str.length;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
if (isVowel(str[j]) == false )
break ;
hash.add(str[j]);
if (hash.size == 5)
document.write(str.substring(i, j + 1) + " " );
}
hash.clear();
}
}
let str = "aeoibsddaeiouudb" ;
findSubstring(str);
</script>
|
Time Complexity : O(n2)
Auxiliary Space: O(1)
Sliding Window Approach -:
Algorithm -:
1. Create a HashSet hash to store vowels.
2. Initialize a variable start to 0.
3. Traverse the string from left to right using a for loop:
a. If the current character is a vowel, add it to the hash.
b. If the size of the hash is 5 (all vowels are present in the current substring), print the substring from start to i+1.
c. If the current character is not a vowel, update the start to i+1 and clear the hash.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' );
}
void FindSubstring(string str)
{
set< char > hash;
int start = 0;
for ( int i=0; i<str.length(); i++)
{
if (isVowel(str[i]) == true )
{
hash.insert(str[i]);
if (hash.size()==5)
cout << str.substr(start, i-start+1)
<< " " ;
}
else
{
start = i+1;
hash.clear();
}
}
}
int main()
{
string str = "aeoibsddaeiouudb" ;
FindSubstring(str);
return 0;
}
|
Java
import java.util.HashSet;
public class GFG {
static boolean isVowel( char x) {
return (x == 'a' || x == 'e' || x == 'i'
|| x == 'o' || x == 'u' );
}
static void findSubstring(String str) {
HashSet<Character> hash = new HashSet<Character>();
int start = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (isVowel(str.charAt(i)) == true ) {
hash.add(str.charAt(i));
if (hash.size() == 5 )
System.out.print(str.substring(start, i + 1 ) + " " );
} else {
start = i + 1 ;
hash.clear();
}
}
}
public static void main(String[] args) {
String str = "aeoibsddaeiouudb" ;
findSubstring(str);
}
}
|
Python3
def isVowel(x):
return (x = = 'a' or x = = 'e' or
x = = 'i' or x = = 'o' or
x = = 'u' );
def FindSubstring( str ):
hash = set ();
start = 0 ;
for i in range ( len ( str )):
if (isVowel( str [i]) = = True ):
hash .add( str [i]);
if ( len ( hash ) = = 5 ):
print ( str [start : i + 1 ],
end = " " );
else :
start = i + 1 ;
hash .clear();
str = "aeoibsddaeiouudb" ;
FindSubstring( str );
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static bool isVowel( char x)
{
return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' );
}
public static void findSubstring( string str)
{
HashSet< char > hash = new HashSet< char >();
int start = 0;
for ( int i = 0; i < str.Length; i++)
{
if (isVowel(str[i]) == true )
{
hash.Add(str[i]);
if (hash.Count == 5)
{
Console.Write(str.Substring(start, (i + 1) - start) + " " );
}
}
else
{
start = i + 1;
hash.Clear();
}
}
}
public static void Main( string [] args)
{
string str = "aeoibsddaeiouudb" ;
findSubstring(str);
}
}
|
Javascript
<script>
function isVowel(x)
{
return (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' );
}
function findSubstring(str)
{
let hash = new Set();
let start = 0;
for (let i = 0; i < str.length; i++)
{
if (isVowel(str[i]) == true )
{
hash.add(str[i]);
if (hash.size == 5)
document.write(
str.substring(start, i + 1) + " " );
}
else
{
start = i + 1;
hash.clear();
}
}
}
let str = "aeoibsddaeiouudb" ;
findSubstring(str);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), the algorithm uses a HashSet to store the vowels, but the size of the HashSet is always 5 regardless of the length of the input string so it use constant space
Thanks to Kriti Shukla for suggesting this optimized solution.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...