Skip to content

Commit 974cd40

Browse files
committed
Add solution #1525
1 parent e3f9816 commit 974cd40

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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,346 LeetCode solutions in JavaScript
1+
# 1,347 LeetCode solutions in JavaScript
22

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

@@ -1165,6 +1165,7 @@
11651165
1521|[Find a Value of a Mysterious Function Closest to Target](./solutions/1521-find-a-value-of-a-mysterious-function-closest-to-target.js)|Hard|
11661166
1523|[Count Odd Numbers in an Interval Range](./solutions/1523-count-odd-numbers-in-an-interval-range.js)|Easy|
11671167
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
1168+
1525|[Number of Good Ways to Split a String](./solutions/1525-number-of-good-ways-to-split-a-string.js)|Medium|
11681169
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|
11691170
1534|[Count Good Triplets](./solutions/1534-count-good-triplets.js)|Easy|
11701171
1535|[Find the Winner of an Array Game](./solutions/1535-find-the-winner-of-an-array-game.js)|Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1525. Number of Good Ways to Split a String
3+
* https://leetcode.com/problems/number-of-good-ways-to-split-a-string/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s.
7+
*
8+
* A split is called good if you can split s into two non-empty strings sleft and sright where
9+
* their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters
10+
* in sleft and sright is the same.
11+
*
12+
* Return the number of good splits you can make in s.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @return {number}
18+
*/
19+
var numSplits = function(s) {
20+
const leftDistinct = new Map();
21+
const rightDistinct = new Map();
22+
let result = 0;
23+
24+
for (const char of s) {
25+
rightDistinct.set(char, (rightDistinct.get(char) || 0) + 1);
26+
}
27+
28+
for (let i = 0; i < s.length - 1; i++) {
29+
const char = s[i];
30+
leftDistinct.set(char, (leftDistinct.get(char) || 0) + 1);
31+
32+
if (rightDistinct.get(char) === 1) {
33+
rightDistinct.delete(char);
34+
} else {
35+
rightDistinct.set(char, rightDistinct.get(char) - 1);
36+
}
37+
38+
if (leftDistinct.size === rightDistinct.size) {
39+
result++;
40+
}
41+
}
42+
43+
return result;
44+
};

0 commit comments

Comments
 (0)