-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumNumberOfVowels.java
81 lines (66 loc) · 2.02 KB
/
MaximumNumberOfVowels.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// The below code passes 102/106 test cases becauses it gives time limit exceeded
class Solution {
public int countVowels(String s) {
Set<Character> vowels = new HashSet<>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
int count = 0;
for (char a : s.toCharArray()) {
if (vowels.contains(a)) {
count++;
}
}
return count;
}
public int maxVowels(String s, int k) {
int length = s.length();
if (length < k) return -1;
int max = 0;
String temp = s.substring(0,k);
max = countVowels(temp);
for(int i=0;i<length-k;i++) {
temp = temp.substring(1) + s.charAt(k+i);
max = Math.max(max, countVowels(temp));
}
return max;
}
}
// The below code used sliding window algorithm and passes all the test cases
import java.util.HashSet;
import java.util.Set;
class Solution {
public int maxVowels(String s, int k) {
Set<Character> vowels = new HashSet<>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');
int length = s.length();
if (length < k) return -1;
int max = 0, currentCount = 0;
// Count vowels in the first window of size k
for (int i = 0; i < k; i++) {
if (vowels.contains(s.charAt(i))) {
currentCount++;
}
}
max = currentCount;
// Slide the window across the string
for (int i = k; i < length; i++) {
// Remove the influence of the character going out of the window
if (vowels.contains(s.charAt(i - k))) {
currentCount--;
}
// Add the influence of the character coming into the window
if (vowels.contains(s.charAt(i))) {
currentCount++;
}
max = Math.max(max, currentCount);
}
return max;
}
}