- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string s (lowercase), we have to find the length of the longest substring where each vowel occurs even number of times.

So, if the input is like s = "anewcoffeepot", then the output will be 10, as the substring "wcoffeepot" has two vowels "o" and "e", both of which occurs two times.

To solve this, we will follow these steps −

vowels := a map assigning vowels and numeric values as {a:0, e:1, i:2, o:3, u:4}

prefix := an empty map and insert a key-value pair (0, −1) into it

mask := 0, n := size of s, res := 0

for i in range 0 to n, do

if s[i] is a vowels, then

mask := mask XOR (2^vowels[s[i]])

if mask is not in prefix, then

prefix[mask] := i

otherwise,

res := maximum of res and (i − prefix[mask])

return res

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s): vowels = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4} prefix = {0: −1} mask = 0 n = len(s) res = 0 for i in range(n): if s[i] in vowels: mask ^= 1 << vowels[s[i]] if mask not in prefix: prefix[mask] = i else: res = max(res, i − prefix[mask]) return res ob = Solution() s = "anewcoffeepot" print(ob.solve(s))

"anewcoffeepot"

10

- Related Questions & Answers
- Find the Longest Substring Containing Vowels in Even Counts in C++
- Program to find length of longest palindromic substring in Python
- Program to find length of longest path with even sum in Python
- Program to find length of longest common substring in C++
- Finding the length of longest vowel substring in a string using JavaScript
- Program to find length of longest palindromic substring after single rotation in Python
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find longest awesome substring in Python
- Program to find the length of longest substring which has two distinct elements in Python
- Program to find longest nice substring using Python
- Program to find length of substring with consecutive common characters in Python
- Program to find length of longest sublist with given condition in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python

Advertisements