Skip to content

Commit 77742be

Browse files
authored
Create 1525-number-of-good-ways-to-split-a-string.js
1 parent 5e3c039 commit 77742be

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const numSplits = function(s) {
6+
const arr = Array(26).fill(0)
7+
const a = 'a'.charCodeAt(0)
8+
for(let i = 0, len = s.length; i < len; i++) {
9+
arr[s.charCodeAt(i) - a]++
10+
}
11+
const cur = Array(26).fill(0)
12+
let res = 0
13+
for(let i = 0, len = s.length; i < len - 1; i++) {
14+
cur[s.charCodeAt(i) - a]++
15+
let tmp = false, clone = arr.slice()
16+
for(let j = 0; j < 26; j++) {
17+
clone[j] -= cur[j]
18+
}
19+
const curNum = cur.reduce((ac, e) => ac + (e > 0 ? 1 : 0), 0)
20+
const cloneNum = clone.reduce((ac, e) => ac + (e > 0 ? 1 : 0), 0)
21+
if(curNum === cloneNum) res++
22+
}
23+
24+
return res
25+
};
26+

0 commit comments

Comments
 (0)