Determine if String Halves Are Alike LeetCode solution in Java
1class Solution {2public boolean halvesAreAlike(String s) {3// add vowels to the set4Set<Character> set = new HashSet<>();5set.add('a');6set.add('e');7set.add('i');8set.add('o');9set.add('u');10set.add('A');11set.add('E');12set.add('I');13set.add('O');14set.add('U');1516// find the mid17int mid = s.length() / 2;18int count = 0;19// increment the count for left half, decrement count for the second half if its a vowel20for (int i = 0; i < s.length(); i++)21count += (set.contains(s.charAt(i))) ? ((i < mid) ? 1 : -1) : 0;22// finally count should be 0 to match left and right half23return count == 0;24}25}