Skip to content

Commit ee13fd4

Browse files
committed
Add solution #1573
1 parent 1b99fa5 commit ee13fd4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-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,376 LeetCode solutions in JavaScript
1+
# 1,377 LeetCode solutions in JavaScript
22

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

@@ -1200,6 +1200,7 @@
12001200
1568|[Minimum Number of Days to Disconnect Island](./solutions/1568-minimum-number-of-days-to-disconnect-island.js)|Hard|
12011201
1569|[Number of Ways to Reorder Array to Get Same BST](./solutions/1569-number-of-ways-to-reorder-array-to-get-same-bst.js)|Hard|
12021202
1572|[Matrix Diagonal Sum](./solutions/1572-matrix-diagonal-sum.js)|Easy|
1203+
1573|[Number of Ways to Split a String](./solutions/1573-number-of-ways-to-split-a-string.js)|Medium|
12031204
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12041205
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12051206
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1573. Number of Ways to Split a String
3+
* https://leetcode.com/problems/number-of-ways-to-split-a-string/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary string s, you can split s into 3 non-empty strings s1, s2, and s3
7+
* where s1 + s2 + s3 = s.
8+
*
9+
* Return the number of ways s can be split such that the number of ones is the same in s1,
10+
* s2, and s3. Since the answer may be too large, return it modulo 109 + 7.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {number}
16+
*/
17+
var numWays = function(s) {
18+
const MOD = 1e9 + 7;
19+
let ones = 0;
20+
21+
for (const char of s) {
22+
if (char === '1') ones++;
23+
}
24+
25+
if (ones % 3 !== 0) return 0;
26+
if (ones === 0) {
27+
return Number((BigInt(s.length - 1) * BigInt(s.length - 2) / 2n) % BigInt(MOD));
28+
}
29+
30+
const targetOnes = ones / 3;
31+
let firstCount = 0;
32+
let secondCount = 0;
33+
let currentOnes = 0;
34+
35+
for (let i = 0; i < s.length; i++) {
36+
if (s[i] === '1') currentOnes++;
37+
if (currentOnes === targetOnes) firstCount++;
38+
else if (currentOnes === 2 * targetOnes) secondCount++;
39+
}
40+
41+
return Number((BigInt(firstCount) * BigInt(secondCount)) % BigInt(MOD));
42+
};

0 commit comments

Comments
 (0)