Problem · String

Longest Substring with Even Occurrences

MediumDeloitteNEW GRADOA

Write a function:

def solution(S)

that, given a string S consisting of N lowercase English letters, returns the length of the longest substring in which every letter occurs an even number of times. A substring is defined as a contiguous segment of a string. If no such substring exists, return 0.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • string S consists only of lowercase letters ('a'-'z').

Examples
01 · Example 1
S = "bdaaadadb"
return = 6
Substrings in which every letter occurs an even number of times are "aa", "adad", "daaadad" and "aaadad". The length of the longest of them is 6.
02 · Example 2
S = "abacb"
return = 0
There is no non-empty substring in which every letter occurs an even number of times.
03 · Example 3
S = "zthtzh"
return = 6
Every letter in the whole string occurs an even number of times.
More Deloitte problems
drafts saved locally
public int solution(String S) {
  // write your code here
}
S"bdaaadadb"
expected6
sign in to submit