- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1456-maximum-number-of-vowels-in-a-substring-of-given-length.rb
51 lines (41 loc) · 1.21 KB
/
1456-maximum-number-of-vowels-in-a-substring-of-given-length.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
# frozen_string_literal: true
# 1456. Maximum Number of Vowels in a Substring of Given Length
# https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length
# Medium
=begin
Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Example 1:
Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.
Example 2:
Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.
Example 3:
Input: s = "leetcode", k = 3
Output: 2
Explanation: "lee", "eet" and "ode" contain 2 vowels.
Constraints:
1 <= s.length <= 105
s consists of lowercase English letters.
1 <= k <= s.length
=end
# @param {String} s
# @param {Integer} k
# @return {Integer}
defmax_vowels(s,k)
(0..s.length - k).map{ |i| s[i..i + k - 1].count("aeiou")}.max
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_max_vowels < Test::Unit::TestCase
deftest_
assert_equal3,max_vowels("abciiidef",3)
assert_equal2,max_vowels("aeiou",2)
assert_equal2,max_vowels("leetcode",3)
end
end