Untitled
unknown
plain_text
2 years ago
539 B
7
Indexable
class Solution {
private static final Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
public boolean halvesAreAlike(String s) {
int balance = 0;
for (int i = 0; i < s.length(); i++) {
boolean isVowel = vowels.contains(s.charAt(i));
if (isVowel) {
if (i < s.length() / 2) {
balance++;
} else {
balance--;
}
}
}
return balance == 0;
}
}Editor is loading...
Leave a Comment