File tree 2 files changed +46
-1
lines changed
2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,346 LeetCode solutions in JavaScript
1
+ # 1,347 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1165
1165
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|
1166
1166
1523|[ Count Odd Numbers in an Interval Range] ( ./solutions/1523-count-odd-numbers-in-an-interval-range.js ) |Easy|
1167
1167
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|
1168
1169
1528|[ Shuffle String] ( ./solutions/1528-shuffle-string.js ) |Easy|
1169
1170
1534|[ Count Good Triplets] ( ./solutions/1534-count-good-triplets.js ) |Easy|
1170
1171
1535|[ Find the Winner of an Array Game] ( ./solutions/1535-find-the-winner-of-an-array-game.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments