-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2489-number-of-substrings-with-fixed-ratio.js
More file actions
53 lines (47 loc) · 1.34 KB
/
2489-number-of-substrings-with-fixed-ratio.js
File metadata and controls
53 lines (47 loc) · 1.34 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
43
44
45
46
47
48
49
50
51
52
53
/**
* 2489. Number of Substrings With Fixed Ratio
* https://leetcode.com/problems/number-of-substrings-with-fixed-ratio/
* Difficulty: Medium
*
* You are given a binary string s, and two integers num1 and num2. num1 and num2 are
* coprime numbers.
*
* A ratio substring is a substring of s where the ratio between the number of 0's and
* the number of 1's in the substring is exactly num1 : num2.
* - For example, if num1 = 2 and num2 = 3, then "01011" and "1110000111" are ratio substrings,
* while "11000" is not.
*
* Return the number of non-empty ratio substrings of s.
*
* Note that:
* - A substring is a contiguous sequence of characters within a string.
* - Two values x and y are coprime if gcd(x, y) == 1 where gcd(x, y) is the greatest common
* divisor of x and y.
*/
/**
* @param {string} s
* @param {number} num1
* @param {number} num2
* @return {number}
*/
var fixedRatio = function(s, num1, num2) {
const n = s.length;
const map = new Map();
map.set(0, 1);
let zeros = 0;
let ones = 0;
let result = 0;
for (let i = 0; i < n; i++) {
if (s[i] === '0') {
zeros++;
} else {
ones++;
}
const difference = ones * num1 - zeros * num2;
if (map.has(difference)) {
result += map.get(difference);
}
map.set(difference, (map.get(difference) || 0) + 1);
}
return result;
};