-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHalves.java
More file actions
25 lines (21 loc) · 904 Bytes
/
StringHalves.java
File metadata and controls
25 lines (21 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public boolean halvesAreAlike(String s) {
int count1 = 0, count2 = 0;
// Convert the string to lowercase
s = s.toLowerCase();
// Iterate through the first half of the string and count vowels
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {
count1++;
}
}
// Iterate through the second half of the string and count vowels
for (int i = s.length() / 2; i < s.length(); i++) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {
count2++;
}
}
// Check if count1 is equal to count2
return count1 == count2;
}
}