class Solution {
public boolean halvesAreAlike(String s) {
Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
int vowelsCount = 0, midIndex = s.length() / 2;
for (int i = 0; i < midIndex; i ) {
char charA = s.charAt(i);
char charB = s.charAt(midIndex i);
if (vowels.contains(charA)) vowelsCount ;
if (vowels.contains(charB)) vowelsCount--;
}
return vowelsCount == 0; //Can you explain this specific return part??
}
}
if was trying to get output as simple return vowelcount but i did not get it? Why I have to use vowelCount==0
CodePudding user response:
Since you are creating a boolean class
public boolean halvesAreAlike(String s)
the return
statement is also supposed to give a value in boolean. But since vowelsCount
is an integer you have to convert it into boolean. To understand the process of converting vowelsCount
into boolean better you can try to replace return vowelsCount == 0;
with below snippet:
.
.
.
Boolean boolValue;
if (vowelsCount >= 1) {
boolValue = false;
}
else {
boolValue = true;
}
return boolValue;
(change logic accordingly) Hope this helps.
CodePudding user response:
As your method in Boolean so we have to return true or false , In that case if vowelsCount is 0 the vowelsCount==0 this statement is true and the method return true and vise versa.