-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaximum-number-of-vowels-in-a-substring-of-given-length.rs
58 lines (56 loc) · 1.82 KB
/
maximum-number-of-vowels-in-a-substring-of-given-length.rs
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
// 1456. Maximum Number of Vowels in a Substring of Given Length
// 🟠 Medium
//
// https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/
//
// Tags: String - Sliding Window
struct Solution;
impl Solution {
/// Use two pointers to iterate over a substring of size k, when the right
/// pointer reads a vowel, it increases the count of vowels in the substring
/// by one, when the left pointer does, it decreases it by one, we return
/// the max count of vowels that we have seen.
///
/// Time complexity: O(n) - We visit once each character in the input and
/// do O(1) work for each.
/// Space complexity: O(n) - We use a vector of chars of the same size as
/// the input. This is the easiest way of iterating over the characters
/// since Rust won't let us index a String type.
///
/// Runtime 10 ms Beats 50%
/// Memory 2.6 MB Beats 50%
pub fn max_vowels(s: String, k: i32) -> i32 {
let k = k as usize;
let chars: Vec<char> = s.chars().collect();
let (mut best, mut cur) = (0, 0);
for r in 0..s.len() {
match chars[r] {
'a' | 'e' | 'i' | 'o' | 'u' => cur += 1,
_ => {}
};
if r >= k {
match chars[r - k] {
'a' | 'e' | 'i' | 'o' | 'u' => cur -= 1,
_ => {}
}
}
if cur > best {
best = cur;
}
}
best as i32
}
}
// Tests.
fn main() {
let tests = [
("abciiidef", 3, 3),
("aeiou", 2, 2),
("leetcode", 3, 2),
("thlslsswprdwlthpvt", 4, 0),
];
for t in tests {
assert_eq!(Solution::max_vowels(String::from(t.0), t.1), t.2);
}
println!("\x1b[92m» All tests passed!\x1b[0m")
}