image_2021-11-07_18-54-02.png
57.8 KB
#N1704. Determine if String Halves Are Alike
problem link
#solution
problem link
#solution
class Solution {
char[] vowels={'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
public boolean halvesAreAlike(String s) {
String half1=s.substring(0, s.length()/2);
String half2=s.substring(s.length()/2);
int count=0;
for(int i=0; i<half1.length(); i++){
if(isVowel(half1.charAt(i)))
count++;
if(isVowel(half2.charAt(i)))
count--;
}
return count==0;
}
boolean isVowel(char ch){
for(char vowel:vowels){
if(ch==vowel)
return true;
}
return false;
}
}