All problems
1110MediumHash TableStringBit ManipulationPrefix Sum

The Longest Stretch With Every Tally Even

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1371Find the Longest Substring Containing Vowels in Even Counts

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A line line holds only lowercase letters. The five letters a, e, i, o and u are the tallied letters, and the other twenty-one are ignored.

A stretch of neighbouring letters is balanced when every tallied letter appears an even number of times inside it, and appearing no times at all counts as even.

Return the length of the longest balanced stretch. The empty stretch is balanced, so the answer is never below zero.

Examples

Example 1

Input
line = "abcabc"
Output
6

The only tallied letter here is a and it turns up twice, so the whole line is balanced.

Example 2

Input
line = "ae"
Output
0

One a and one e, each an odd number of times, and dropping either letter leaves the other odd, so nothing but the empty stretch is balanced.

Example 3

Input
line = "azbzc"
Output
4

The single a is tallied once, so a balanced stretch has to leave it out. The four letters after it hold no tallied letter at all.

Constraints

  • 1 <= line.length <= 5 * 10^5
  • line holds only lowercase English letters

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def find_the_longest_substring(line: str) -> int:
Java
public int findTheLongestSubstring(String line)
September 7
Apply