Given a string S consisting of N lowercase characters, the task is to find the length of the longest substring consisting of each vowel an even number of times
Examples:
Input: S= “bcbcbc”
Output: 6
Explanation:
Consider the substring S[0, 5] i.e., “bcbcbc” is the longest substring because all vowels: a, e, i, o and u appear 0(which is even) number of times.
Input: S = “ebbaa”
Output: 4
Naive Approach: The simplest approach to solve the given problem is to generate all possible substrings from the given string S and for each substring, check if the frequency of all vowels in the substring is even or not. If found to be true, then update the maximum length of the string required. After checking for all the substrings, print the maximum length obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Hashing. The idea is to initialize a string, say temp of size 5 corresponding to the 5 vowels (a, e, i, o, u) with all 0s, where s[i] = 0 indicates that the ith vowel is occurring an even number of times. Now, traverse the given string and find the same state of the string, temp from the map and update the maximum length. Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0 that stores the required result.
- Initialize a string, say temp of size 5 as “00000”.
- Create a hashmap, M to store the index of occurrence of string temp and initialize the value of temp in M as -1.
- Traverse the given string S over the range [0, N – 1] using the variable i and perform the following steps:
- If the character S[i] is a vowel, then update the string temp.
- If the string temp is present in the map M then store its value from the map M in a variable X and update the value of ans to the maximum of ans and (i – X).
- Otherwise, update the value of temp in the map M as i.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubstring(string s)
{
unordered_map<string, int > indexes;
unordered_map< char , int > chars(
{ { 'a' , 0 }, { 'e' , 1 },
{ 'i' , 2 }, { 'o' , 3 },
{ 'u' , 4 } });
string evenOdd = "00000" ;
indexes[evenOdd] = -1;
int length = 0;
for ( int i = 0; i < s.size(); ++i) {
char c = s[i];
auto it = chars.find(c);
if (it != chars.end()) {
evenOdd[it->second]
= evenOdd[it->second]
== '0'
? '1'
: '0' ;
}
auto lastIndex = indexes.find(evenOdd);
if (lastIndex == indexes.end()) {
indexes[evenOdd] = i;
}
else {
length = max(
length, i - lastIndex->second);
}
}
cout << length;
}
int main()
{
string S = "bcbcbc" ;
longestSubstring(S);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void longestSubstring(String s)
{
HashMap<String, Integer> indexes = new HashMap<>();
HashMap<Character, Integer> chars = new HashMap<>(){{
put( 'a' , 0 );put( 'e' , 1 );
put( 'i' , 2 );put( 'o' , 3 );
put( 'u' , 4 );
}} ;
String evenOdd = "00000" ;
indexes.put(evenOdd , - 1 );
int length = 0 ;
for ( int i = 0 ; i < s.length(); ++i) {
char c = s.charAt(i);
boolean it = chars.containsKey(c);
if (it != false ) {
if (evenOdd.charAt(chars.get(c)) == '0' ){
evenOdd = evenOdd.substring( 0 ,chars.get(c)) + '1' + evenOdd.substring(chars.get(c)+ 1 );
}
else {
evenOdd = evenOdd.substring( 0 ,chars.get(c)) + '0' + evenOdd.substring(chars.get(c)+ 1 );
}
}
boolean lastIndex = indexes.containsKey(evenOdd);
if (lastIndex == false ) {
indexes.put(evenOdd, i);
}
else {
length = Math.max(length, i - indexes.get(evenOdd));
}
}
System.out.println(length);
}
public static void main(String args[])
{
String S = "bcbcbc" ;
longestSubstring(S);
}
}
|
Python3
def longestSubstring(s):
indexes = {}
chars = {}
chars[ 'a' ] = 0
chars[ 'e' ] = 1
chars[ 'i' ] = 2
chars[ 'o' ] = 3
chars[ 'u' ] = 4
evenOdd = "00000"
evenOdd = [i for i in evenOdd]
indexes["".join(evenOdd)] = - 1
length = 0
for i in range ( len (s)):
c = s[i]
if (c in chars):
evenOdd[chars[it]] = '1' if evenOdd[chars[it]] else '0'
if ("".join(evenOdd) not in indexes):
indexes["".join(evenOdd)] = i
else :
length = max (
length, i - indexes["".join(evenOdd)])
print (length)
if __name__ = = '__main__' :
S = "bcbcbc"
longestSubstring(S)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void LongestSubstring( string s)
{
Dictionary< string , int > indexes = new Dictionary< string , int >();
Dictionary< char , int > chars = new Dictionary< char , int >()
{
{ 'a' , 0}, { 'e' , 1},
{ 'i' , 2}, { 'o' , 3},
{ 'u' , 4}
};
string evenOdd = "00000" ;
indexes[evenOdd] = -1;
int length = 0;
for ( int i = 0; i < s.Length; ++i)
{
char c = s[i];
bool it = chars.ContainsKey(c);
if (it != false )
{
if (evenOdd[chars] == '0' )
{
evenOdd = evenOdd.Substring(0, chars) + '1' + evenOdd.Substring(chars + 1);
}
else
{
evenOdd = evenOdd.Substring(0, chars) + '0' + evenOdd.Substring(chars + 1);
}
}
bool lastIndex = indexes.ContainsKey(evenOdd);
if (lastIndex == false )
{
indexes[evenOdd] = i;
}
else
{
length = Math.Max(length, i - indexes[evenOdd]);
}
}
Console.WriteLine(length);
}
static void Main( string [] args)
{
string S = "bcbcbc" ;
LongestSubstring(S);
}
}
|
Javascript
<script>
function longestSubstring(s)
{
let indexes = new Map();
let chars = new Map();
chars.set( 'a' ,0);
chars.set( 'e' ,1);
chars.set( 'i' ,2);
chars.set( 'o' ,3);
chars.set( 'u' ,4);
let evenOdd = "00000" ;
indexes.set(evenOdd , -1);
let length = 0;
for (let i = 0; i <br s.length; ++i) {
let c = s[i];
if (chars.has(c)) {
evenOdd.set(chars.get(c),(evenOdd.get(chars.get(c)) == '0' )? '1' : '0' );
}
if (indexes.has(evenOdd) == false ) {
indexes.set(evenOdd,i);
}
else {
length = Math.max(length, i - indexes.get(evenOdd));
}
}
document.write(length, "</br>" );
}
let S = "bcbcbc" ;
longestSubstring(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jul, 2023
Like Article
Save Article