Skip to content

Commit 29b7fe0

Browse files
committed
Add solution #1704
1 parent eed3795 commit 29b7fe0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,488 LeetCode solutions in JavaScript
1+
# 1,489 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1313,6 +1313,7 @@
13131313
1701|[Average Waiting Time](./solutions/1701-average-waiting-time.js)|Medium|
13141314
1702|[Maximum Binary String After Change](./solutions/1702-maximum-binary-string-after-change.js)|Medium|
13151315
1703|[Minimum Adjacent Swaps for K Consecutive Ones](./solutions/1703-minimum-adjacent-swaps-for-k-consecutive-ones.js)|Hard|
1316+
1704|[Determine if String Halves Are Alike](./solutions/1704-determine-if-string-halves-are-alike.js)|Easy|
13161317
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13171318
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13181319
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1704. Determine if String Halves Are Alike
3+
* https://leetcode.com/problems/determine-if-string-halves-are-alike/
4+
* Difficulty: Easy
5+
*
6+
* You are given a string s of even length. Split this string into two halves of equal lengths,
7+
* and let a be the first half and b be the second half.
8+
*
9+
* Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A',
10+
* 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.
11+
*
12+
* Return true if a and b are alike. Otherwise, return false.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @return {boolean}
18+
*/
19+
var halvesAreAlike = function(s) {
20+
const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
21+
let firstHalfVowels = 0;
22+
let secondHalfVowels = 0;
23+
const mid = s.length / 2;
24+
25+
for (let i = 0; i < mid; i++) {
26+
if (vowels.has(s[i])) firstHalfVowels++;
27+
if (vowels.has(s[i + mid])) secondHalfVowels++;
28+
}
29+
30+
return firstHalfVowels === secondHalfVowels;
31+
};

0 commit comments

Comments
 (0)