-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1371-find-the-longest-substring-containing-vowels-in-even-counts.rb
65 lines (53 loc) · 1.71 KB
/
1371-find-the-longest-substring-containing-vowels-in-even-counts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
# 1371. Find the Longest Substring Containing Vowels in Even Counts
# Medium
# https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts
=begin
Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.
Example 1:
Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
Example 2:
Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.
Example 3:
Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.
Constraints:
1 <= s.length <= 5 x 10^5
s contains only lowercase English letters.
=end
# @param {String} s
# @return {Integer}
def find_the_longest_substring(s)
vowels = "aeiou"
state = 0
seen = { 0 => -1 }
max_length = 0
s.chars.each_with_index do |char, index|
vowel_index = vowels.index(char)
if vowel_index
state ^= 1 << vowel_index
end
if seen.key?(state)
max_length = [max_length, index - seen[state]].max
else
seen[state] = index
end
end
max_length
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_find_the_longest_substring < Test::Unit::TestCase
def test_
assert_equal 13, find_the_longest_substring("eleetminicoworoep")
assert_equal 5, find_the_longest_substring("leetcodeisgreat")
assert_equal 6, find_the_longest_substring("bcbcbc")
end
end