Skip to content

Commit d02685c

Browse files
add 1456
1 parent 4c02c21 commit d02685c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|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/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1456.java) | |Easy|String, Sliding Window|
1112
|1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1455.java) | |Easy|String|
1213
|1452|[People Whose List of Favorite Companies Is Not a Subset of Another List](https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1452.java) | |Medium|String, Sort|
1314
|1451|[Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1451.java) | |Medium|String, Sort|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class _1456 {
9+
public static class Solution1 {
10+
public int maxVowels(String s, int k) {
11+
Map<Character, Integer> vowels = new HashMap<>();
12+
Set<Character> set = new HashSet<>();
13+
set.add('a');
14+
set.add('e');
15+
set.add('i');
16+
set.add('o');
17+
set.add('u');
18+
int left = 0;
19+
for (; left < k; left++) {
20+
if (set.contains(s.charAt(left))) {
21+
vowels.put(s.charAt(left), vowels.getOrDefault(s.charAt(left), 0) + 1);
22+
}
23+
}
24+
int max = 0;
25+
for (char c : vowels.keySet()) {
26+
max += vowels.get(c);
27+
}
28+
int right = left;
29+
left = 0;
30+
for (; right < s.length(); right++, left++) {
31+
char leftChar = s.charAt(left);
32+
if (set.contains(leftChar) && vowels.containsKey(leftChar) && vowels.get(leftChar) > 0) {
33+
vowels.put(leftChar, vowels.get(leftChar) - 1);
34+
}
35+
char rightChar = s.charAt(right);
36+
if (set.contains(rightChar)) {
37+
vowels.put(rightChar, vowels.getOrDefault(rightChar, 0) + 1);
38+
}
39+
int thisMax = 0;
40+
for (char c : vowels.keySet()) {
41+
thisMax += vowels.get(c);
42+
}
43+
max = Math.max(max, thisMax);
44+
}
45+
return max;
46+
}
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1456;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1456Test {
10+
private static _1456.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1456.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.maxVowels("abciiidef", 3));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)