Longest Substring having equal count of Vowels and Consonants
Given a string S consisting of lowercase English letters, the task is to find the length of the longest substring from the given string, having an equal number of vowels and consonants.
Examples:
Input: S = “geeksforgeeks”
Output: 10
Explanation:
The substring “eeksforgee” consists of 5 vowels and 5 consonants. Remaining characters are only consonants. Therefore, any longer substring won’t have an equal number of vowels and consonants.
Input: S = “qwertyuiop”
Output: 8
Explanation:
The substring “wertyuio” consists of 4 vowels and 4 consonants.
Naive Approach: The simplest solution is to generate all substrings of the given string and for each substring, check if the count of vowels and consonants are equal or not. Finally, print the maximum length of substring obtained having an equal number of vowels and consonants.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The idea is to consider an array of lengths equal to that of the given string, storing 1 and -1 corresponding to vowels and consonants respectively, and print the length of the longest sub-array with the sum equal to 0 using HashMap.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxsubstringLength(string S, int N)
{
int arr[N];
for ( int i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' || S[i] == 'i'
|| S[i] == 'o' || S[i] == 'u' )
arr[i] = 1;
else
arr[i] = -1;
int maxLen = 0;
int curr_sum = 0;
unordered_map< int , int > hash;
for ( int i = 0; i < N; i++) {
curr_sum += arr[i];
if (curr_sum == 0)
maxLen = max(maxLen, i + 1);
if (hash.find(curr_sum) != hash.end())
maxLen = max(maxLen, i - hash[curr_sum]);
else
hash[curr_sum] = i;
}
return maxLen;
}
int main()
{
string S = "geeksforgeeks" ;
int n = sizeof (S) / sizeof (S[0]);
cout << maxsubstringLength(S, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxsubStringLength( char [] S, int N)
{
int arr[] = new int [N];
for ( int i = 0 ; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' ||
S[i] == 'i' || S[i] == 'o' ||
S[i] == 'u' )
arr[i] = 1 ;
else
arr[i] = - 1 ;
int maxLen = 0 ;
int curr_sum = 0 ;
HashMap<Integer, Integer> hash = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
curr_sum += arr[i];
if (curr_sum == 0 )
maxLen = Math.max(maxLen, i + 1 );
if (hash.containsKey(curr_sum))
maxLen = Math.max(maxLen,
i - hash.get(curr_sum));
else
hash.put(curr_sum, i);
}
return maxLen;
}
public static void main(String[] args)
{
String S = "geeksforgeeks" ;
int n = S.length();
System.out.print(
maxsubStringLength(S.toCharArray(), n));
}
}
|
Python3
def maxsubstringLength(S, N):
arr = [ 0 ] * N
for i in range (N):
if (S[i] = = 'a' or S[i] = = 'e' or
S[i] = = 'i' or S[i] = = 'o' or
S[i] = = 'u' ):
arr[i] = 1
else :
arr[i] = - 1
maxLen = 0
curr_sum = 0
hash = {}
for i in range (N):
curr_sum + = arr[i]
if (curr_sum = = 0 ):
maxLen = max (maxLen, i + 1 )
if (curr_sum in hash .keys()):
maxLen = max (maxLen, i - hash [curr_sum])
else :
hash [curr_sum] = i
return maxLen
S = "geeksforgeeks"
n = len (S)
print (maxsubstringLength(S, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maxsubStringLength( char [] S, int N)
{
int []arr = new int [N];
for ( int i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' ||
S[i] == 'i' || S[i] == 'o' ||
S[i] == 'u' )
arr[i] = 1;
else
arr[i] = -1;
int maxLen = 0;
int curr_sum = 0;
Dictionary< int ,
int > hash = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
curr_sum += arr[i];
if (curr_sum == 0)
maxLen = Math.Max(maxLen, i + 1);
if (hash.ContainsKey(curr_sum))
maxLen = Math.Max(maxLen,
i - hash[curr_sum]);
else
hash.Add(curr_sum, i);
}
return maxLen;
}
public static void Main(String[] args)
{
String S = "geeksforgeeks" ;
int n = S.Length;
Console.Write(maxsubStringLength(
S.ToCharArray(), n));
}
}
|
Javascript
<script>
function maxsubStringLength(S, N)
{
let arr = Array.from({length: N}, (_, i) => 0);
for (let i = 0; i < N; i++)
if (S[i] == 'a' || S[i] == 'e' ||
S[i] == 'i' || S[i] == 'o' ||
S[i] == 'u' )
arr[i] = 1;
else
arr[i] = -1;
let maxLen = 0;
let curr_sum = 0;
let hash = new Map();
for (let i = 0; i < N; i++)
{
curr_sum += arr[i];
if (curr_sum == 0)
maxLen = Math.max(maxLen, i + 1);
if (hash.has(curr_sum))
maxLen = Math.max(maxLen,
i - hash.get(curr_sum));
else
hash.set(curr_sum, i);
}
return maxLen;
}
let S = "geeksforgeeks" ;
let n = S.length;
document.write(maxsubStringLength(S.split( '' ), n));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
21 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...