Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd02685c

Browse files
add 1456
1 parent4c02c21 commitd02685c

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.HashSet;
5+
importjava.util.Map;
6+
importjava.util.Set;
7+
8+
publicclass_1456 {
9+
publicstaticclassSolution1 {
10+
publicintmaxVowels(Strings,intk) {
11+
Map<Character,Integer>vowels =newHashMap<>();
12+
Set<Character>set =newHashSet<>();
13+
set.add('a');
14+
set.add('e');
15+
set.add('i');
16+
set.add('o');
17+
set.add('u');
18+
intleft =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+
intmax =0;
25+
for (charc :vowels.keySet()) {
26+
max +=vowels.get(c);
27+
}
28+
intright =left;
29+
left =0;
30+
for (;right <s.length();right++,left++) {
31+
charleftChar =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+
charrightChar =s.charAt(right);
36+
if (set.contains(rightChar)) {
37+
vowels.put(rightChar,vowels.getOrDefault(rightChar,0) +1);
38+
}
39+
intthisMax =0;
40+
for (charc :vowels.keySet()) {
41+
thisMax +=vowels.get(c);
42+
}
43+
max =Math.max(max,thisMax);
44+
}
45+
returnmax;
46+
}
47+
}
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1456;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.TestCase.assertEquals;
8+
9+
publicclass_1456Test {
10+
privatestatic_1456.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1456.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(3,solution1.maxVowels("abciiidef",3));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp