Skip to content

Commit ecfdcae

Browse files
solves determine if string halves are alike
1 parent bb7925f commit ecfdcae

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@
418418
| 1688 | [Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament) | [![Java](assets/java.png)](src/CountOfMatchesInTournament.java) | |
419419
| 1694 | [Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number) | [![Java](assets/java.png)](src/ReformatPhoneNumber.java) | |
420420
| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch) | [![Java](assets/java.png)](src/NumberOfStudentsUnableToEatLunch.java) | |
421-
| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike) | | |
421+
| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike) | [![Java](assets/java.png)](src/DetermineIfStringHalvesAreAlike.java) | |
422422
| 1708 | 🔒 [Largest Subarray Length K](https://leetcode.com/problems/largest-subarray-length-k/) | | |
423423
| 1710 | [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | | |
424424
| 1716 | [Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank) | | |

Diff for: src/DetermineIfStringHalvesAreAlike.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.Set;
2+
3+
public class DetermineIfStringHalvesAreAlike {
4+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
5+
6+
public boolean halvesAreAlike(String s) {
7+
return numberOfVowels(s, 0, s.length() / 2) == numberOfVowels(s, s.length() / 2);
8+
}
9+
10+
private int numberOfVowels(String s, int startIndex) {
11+
return numberOfVowels(s, startIndex, s.length());
12+
}
13+
14+
private int numberOfVowels(String s, int startIndex, int endIndex) {
15+
int count = 0;
16+
for (int index = startIndex ; index < endIndex ; index++) {
17+
if (VOWELS.contains(s.charAt(index))) {
18+
count++;
19+
}
20+
}
21+
return count;
22+
}
23+
}

0 commit comments

Comments
 (0)