Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
539 B
2
Indexable
Never
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;
    }
}
Leave a Comment