Check whether all the substrings have number of vowels atleast as that of consonants
Last Updated :
28 Jul, 2022
Given a string str, the task is to check whether all the substrings of length ? 2 have the number of vowels at least as that of the number of consonants.
Examples:
Input: str = “acaba”
Output: No
The substring “cab” has 2 consonants and a single vowel.
Input: str = “aabaa”
Output: Yes
Approach: There are only two cases where the given condition is not satisfied:
- When there are two consecutive consonants as in this case a substring of size 2 can have 2 consonants and no vowels.
- When there is a vowel surrounded by two consonants, in this case a substring of length 3 is possible with 2 consonants and 1 vowels.
All the other cases will always satisfy the given conditions.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
switch (ch) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
}
return false ;
}
bool isSatisfied(string str, int n)
{
for ( int i = 1; i < n; i++) {
if (!isVowel(str[i])
&& !isVowel(str[i - 1])) {
return false ;
}
}
for ( int i = 1; i < n - 1; i++) {
if (isVowel(str[i])
&& !isVowel(str[i - 1])
&& !isVowel(str[i + 1])) {
return false ;
}
}
return true ;
}
int main()
{
string str = "acaba" ;
int n = str.length();
if (isSatisfied(str, n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isVowel( char ch)
{
switch (ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
}
return false ;
}
static boolean isSatisfied( char [] str, int n)
{
for ( int i = 1 ; i < n; i++)
{
if (!isVowel(str[i]) &&
!isVowel(str[i - 1 ]))
{
return false ;
}
}
for ( int i = 1 ; i < n - 1 ; i++)
{
if (isVowel(str[i]) &&
!isVowel(str[i - 1 ]) &&
!isVowel(str[i + 1 ]))
{
return false ;
}
}
return true ;
}
public static void main(String []args)
{
String str = "acaba" ;
int n = str.length();
if (isSatisfied(str.toCharArray(), n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isVowel(ch):
if ch in [ 'i' , 'a' , 'e' , 'o' , 'u' ]:
return True
else :
return False
def isSatisfied(st, n):
for i in range ( 1 , n):
if (isVowel(st[i]) = = False and
isVowel(st[i - 1 ]) = = False ):
return False
for i in range ( 1 , n - 1 ):
if (isVowel(st[i]) and
isVowel(st[i - 1 ]) = = False and
isVowel(st[i + 1 ]) = = False ):
return False
return True
st = "acaba"
n = len (st)
if (isSatisfied(st, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isVowel( char ch)
{
switch (ch)
{
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' :
return true ;
}
return false ;
}
static bool isSatisfied( char [] str, int n)
{
for ( int i = 1; i < n; i++)
{
if (!isVowel(str[i]) &&
!isVowel(str[i - 1]))
{
return false ;
}
}
for ( int i = 1; i < n - 1; i++)
{
if (isVowel(str[i]) &&
!isVowel(str[i - 1]) &&
!isVowel(str[i + 1]))
{
return false ;
}
}
return true ;
}
public static void Main(String []args)
{
String str = "acaba" ;
int n = str.Length;
if (isSatisfied(str.ToCharArray(), n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isVowel(ch) {
switch (ch) {
case "a" :
case "e" :
case "i" :
case "o" :
case "u" :
return true ;
}
return false ;
}
function isSatisfied(str, n) {
for ( var i = 1; i < n; i++) {
if (!isVowel(str[i]) && !isVowel(str[i - 1])) {
return false ;
}
}
for ( var i = 1; i < n - 1; i++) {
if (isVowel(str[i]) && !isVowel(str[i - 1]) && !isVowel(str[i + 1])) {
return false ;
}
}
return true ;
}
var str = "acaba" ;
var n = str.length;
if (isSatisfied(str.split( "" ), n)) document.write( "Yes" );
else document.write( "No" );
</script>
|
Time Complexity : O( | str | ) ,where | str | is length of given string str.
Auxiliary Space : O(1) ,as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...