Skip to content

Commit 8e10066

Browse files
committed
Add solution #1297
1 parent 6c39ed1 commit 8e10066

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1297. Maximum Number of Occurrences of a Substring
3+
* https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, return the maximum number of ocurrences of
7+
* any substring under the following rules:
8+
*
9+
* - The number of unique characters in the substring must be less
10+
* than or equal to maxLetters.
11+
* - The substring size must be between minSize and maxSize inclusive.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @param {number} maxLetters
17+
* @param {number} minSize
18+
* @param {number} maxSize
19+
* @return {number}
20+
*/
21+
var maxFreq = function(s, maxLetters, minSize, maxSize) {
22+
const occurrences = new Map();
23+
24+
for (let i = 0; i <= s.length - minSize; i++) {
25+
const string = s.substr(i, minSize);
26+
if (new Set(string.split('')).size <= maxLetters) {
27+
occurrences.set(string, occurrences.has(string) ? occurrences.get(string) + 1 : 1);
28+
}
29+
}
30+
31+
return (Array.from(occurrences).sort((a, b) => b[1] - a[1])[0] || [])[1] || 0;
32+
};

0 commit comments

Comments
 (0)