Skip to content

Commit 51f7710

Browse files
update 1456
1 parent 82d1b7b commit 51f7710

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

  • src/main/java/com/fishercoder/solutions/secondthousand

‎src/main/java/com/fishercoder/solutions/secondthousand/_1456.java‎

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
package com.fishercoder.solutions.secondthousand;
22

3-
import java.util.Arrays;
4-
import java.util.HashMap;
53
import java.util.HashSet;
6-
import java.util.Map;
74
import java.util.Set;
85

96
public class _1456 {
107
public static class Solution1 {
118
public int maxVowels(String s, int k) {
12-
Map<Character, Integer> vowels = new HashMap<>();
13-
Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
14-
int left = 0;
15-
for (; left < k; left++) {
16-
if (set.contains(s.charAt(left))) {
17-
vowels.put(s.charAt(left), vowels.getOrDefault(s.charAt(left), 0) + 1);
9+
Set<Character> set = new HashSet<>();
10+
set.add('a');
11+
set.add('e');
12+
set.add('i');
13+
set.add('o');
14+
set.add('u');
15+
int max = Integer.MIN_VALUE;
16+
int i = 0;
17+
int vowels = 0;
18+
for (; i < k; i++) {
19+
if (set.contains(s.charAt(i))) {
20+
vowels++;
1821
}
1922
}
20-
int max = 0;
21-
for (char c : vowels.keySet()) {
22-
max += vowels.get(c);
23-
}
24-
int right = left;
25-
left = 0;
26-
for (; right < s.length(); right++, left++) {
27-
char leftChar = s.charAt(left);
28-
if (set.contains(leftChar)
29-
&& vowels.containsKey(leftChar)
30-
&& vowels.get(leftChar) > 0) {
31-
vowels.put(leftChar, vowels.get(leftChar) - 1);
32-
}
33-
char rightChar = s.charAt(right);
34-
if (set.contains(rightChar)) {
35-
vowels.put(rightChar, vowels.getOrDefault(rightChar, 0) + 1);
23+
max = Math.max(max, vowels);
24+
for (; i < s.length(); i++) {
25+
if (set.contains(s.charAt(i))) {
26+
vowels++;
3627
}
37-
int thisMax = 0;
38-
for (char c : vowels.keySet()) {
39-
thisMax += vowels.get(c);
28+
if (set.contains(s.charAt(i - k))) {
29+
vowels--;
4030
}
41-
max = Math.max(max, thisMax);
31+
max = Math.max(max, vowels);
4232
}
4333
return max;
4434
}

0 commit comments

Comments
 (0)