-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1573-number-of-ways-to-split-a-string.js
More file actions
42 lines (36 loc) · 1.09 KB
/
1573-number-of-ways-to-split-a-string.js
File metadata and controls
42 lines (36 loc) · 1.09 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 1573. Number of Ways to Split a String
* https://leetcode.com/problems/number-of-ways-to-split-a-string/
* Difficulty: Medium
*
* Given a binary string s, you can split s into 3 non-empty strings s1, s2, and s3
* where s1 + s2 + s3 = s.
*
* Return the number of ways s can be split such that the number of ones is the same in s1,
* s2, and s3. Since the answer may be too large, return it modulo 109 + 7.
*/
/**
* @param {string} s
* @return {number}
*/
var numWays = function(s) {
const MOD = 1e9 + 7;
let ones = 0;
for (const char of s) {
if (char === '1') ones++;
}
if (ones % 3 !== 0) return 0;
if (ones === 0) {
return Number((BigInt(s.length - 1) * BigInt(s.length - 2) / 2n) % BigInt(MOD));
}
const targetOnes = ones / 3;
let firstCount = 0;
let secondCount = 0;
let currentOnes = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === '1') currentOnes++;
if (currentOnes === targetOnes) firstCount++;
else if (currentOnes === 2 * targetOnes) secondCount++;
}
return Number((BigInt(firstCount) * BigInt(secondCount)) % BigInt(MOD));
};