Skip to content

Commit f7fd40e

Browse files
authored
Update Determine if String Halves Are Alike.java
1 parent f3474fb commit f7fd40e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
class Solution {
2-
private static final String VOWELS = "aeiouAEIOU";
3-
public boolean halvesAreAlike(String s) {
4-
return getVowelCount(s.substring(0, s.length() / 2)).equals(getVowelCount(s.substring(s.length() / 2)));
5-
}
6-
7-
private Long getVowelCount(String s) {
8-
return s.chars().mapToObj(c -> (char) c).filter(Solution::isVowel).count();
9-
}
10-
11-
private static boolean isVowel(char c) {
12-
return VOWELS.indexOf(c) != -1;
13-
}
2+
3+
private static final Set<Character> VOWELS = Set.of(
4+
'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'
5+
);
6+
7+
public boolean halvesAreAlike(String s) {
8+
int count = 0;
9+
int n = s.length();
10+
for (int i = 0; i < n / 2; i++) {
11+
if (VOWELS.contains(s.charAt(i))) {
12+
count++;
13+
}
14+
if (VOWELS.contains(s.charAt(i + n / 2))) {
15+
count--;
16+
}
17+
}
18+
return count == 0;
19+
}
1420
}

0 commit comments

Comments
 (0)