Skip to content

Commit 9a1c531

Browse files
authored
Create 1704-determine-if-string-halves-are-alike.js
1 parent 9d43c25 commit 9a1c531

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const halvesAreAlike = function(s) {
6+
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
7+
const n = s.length
8+
const mid = n / 2
9+
const first = s.slice(0, mid), second = s.slice(mid)
10+
return chk(first, set) === chk(second, set)
11+
};
12+
13+
function chk(str, set) {
14+
let res = 0
15+
for(let i = 0, len = str.length; i < len; i++) {
16+
if(set.has(str[i])) res++
17+
}
18+
return res
19+
}

0 commit comments

Comments
 (0)