Maximize count of Substrings containing at least 1 vowel and 1 consonant
Last Updated :
30 Aug, 2022
Given a string S consisting of only lowercase English letters of length N, the task is to find the count of substrings such that each substring contains at least 1 vowel and 1 consonant.
Examples:
Input: S = “happybirthday”
Output: 3
Explanation: S can be divided as “ha”, “ppybi”, “rthday”
Input: S = “geeksforgeeks”
Output 5
Explanation: S can be divided as “ge“, “ek“, “sfo“, “rge”, “eks”
Approach: To solve the problem follow the below idea:
The idea is to apply the greedy approach, traverse the string, and every time we encounter 1 vowel and 1 consonant increase the count by 1 and reset the number of vowels and consonants to 0.
Follow the steps below to solve the problem:
- Initialize variable ans = 0, haveVowels = false (current string have vowels or not), haveConsonants = false (current string have consonants or not)
- Travel over the string by each character
- If current character is vowels make the haveVowels = true else haveConsonants = true
- Whenever both are true then consider current subsegment is a valid substring, increase ans by 1, make haveVowels = false and haveConsonants = false.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int strengthOfString(string S)
{
int ans = 0;
int N = S.size();
bool haveVowels = false ;
bool haveConsonants = false ;
for ( int i=0;i<N;i++)
{
if (S[i]== 'a' ||S[i]== 'e' ||S[i]== 'i' ||S[i]== 'o' ||S[i]== 'u' )
haveVowels = true ;
else
haveConsonants = true ;
if (haveVowels && haveConsonants)
{
ans += 1;
haveVowels = false ;
haveConsonants = false ;
}
}
return ans;
}
int main()
{
string S = "happybirthday" ;
int ans = strengthOfString(S);
cout << ans;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int strengthOfString(String S)
{
int ans = 0 ;
int N = S.length();
boolean haveVowels = false ;
boolean haveConsonants = false ;
for ( int i = 0 ; i < N; i++) {
if (S.charAt(i) == 'a' || S.charAt(i) == 'e'
|| S.charAt(i) == 'i' || S.charAt(i) == 'o'
|| S.charAt(i) == 'u' )
haveVowels = true ;
else
haveConsonants = true ;
if (haveVowels == true
&& haveConsonants == true ) {
ans += 1 ;
haveVowels = false ;
haveConsonants = false ;
}
}
return ans;
}
public static void main(String[] args)
{
String S = "happybirthday" ;
int ans = strengthOfString(S);
System.out.print(ans);
}
}
|
Python3
def strengthOfString(S):
ans = 0
N = len (S)
haveVowels = False
haveConsonants = False
for s in S:
if s in "aeiou" :
haveVowels = True
else :
haveConsonants = True
if haveVowels and haveConsonants:
ans + = 1
haveVowels = False
haveConsonants = False
return ans
if __name__ = = "__main__" :
S = "happybirthday"
ans = strengthOfString(S)
print (ans)
|
C#
using System;
public class GFG {
public static int strengthOfString(String S)
{
int ans = 0;
int N = S.Length;
bool haveVowels = false ;
bool haveConsonants = false ;
for ( int i = 0; i < N; i++) {
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' )
haveVowels = true ;
else
haveConsonants = true ;
if (haveVowels == true
&& haveConsonants == true ) {
ans += 1;
haveVowels = false ;
haveConsonants = false ;
}
}
return ans;
}
static public void Main()
{
string S = "happybirthday" ;
int ans = strengthOfString(S);
Console.Write(ans);
}
}
|
Javascript
<script>
function strengthOfString(S)
{
let ans = 0;
let N = S.length;
let haveVowels = false ;
let haveConsonants = false ;
for (let i = 0; i < N; i++) {
if (S.charAt(i) == 'a' || S.charAt(i) == 'e'
|| S.charAt(i) == 'i' || S.charAt(i) == 'o'
|| S.charAt(i) == 'u' )
haveVowels = true ;
else
haveConsonants = true ;
if (haveVowels == true
&& haveConsonants == true ) {
ans += 1;
haveVowels = false ;
haveConsonants = false ;
}
}
return ans;
}
let S = "happybirthday" ;
let ans = strengthOfString(S);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...