Skip to content

Commit cd14d4e

Browse files
add 1297
1 parent bddaa21 commit cd14d4e

File tree

7 files changed

+117
-4
lines changed

7 files changed

+117
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,5 @@ LeetCode
594594
|1292|[Maximum Side Length of a Square with Sum Less than or Equal to Threshold](https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/)|c|[c++](./src/1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292.cpp)|[python](./src/1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292.py)|[go](./src/1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292.go)|[js](./src/1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292.js)|[java](./src/1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292.java)|Medium|
595595
|1293|[Shortest Path in a Grid with Obstacles Elimination](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/)|c|[c++](./src/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/1293.cpp)|[python](./src/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/1293.py)|[go](./src/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/1293.go)|[js](./src/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/1293.js)|[java](./src/1293-Shortest-Path-in-a-Grid-with-Obstacles-Elimination/1293.java)|Hard|
596596
|1295|[Find Numbers with Even Number of Digits](https://leetcode.com/problems/find-numbers-with-even-number-of-digits/)|c|[c++](./src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.cpp)|[python](./src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.py)|[go](./src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.go)|[js](./src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.js)|[java](./src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.java)|Easy|
597-
|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|c|[c++](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.cpp)|[python](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.py)|[go](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.go)|[js](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.js)|[java](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.java)|Medium|
597+
|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|c|[c++](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.cpp)|[python](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.py)|[go](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.go)|[js](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.js)|[java](./src/1296-Divide-Array-in-Sets-of-K-Consecutive-Numbers/1296.java)|Medium|
598+
|1297|[Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/)|c|[c++](./src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.cpp)|[python](./src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.py)|[go](./src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.go)|[js](./src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.js)|[java](./src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.java)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution
2+
{
3+
public:
4+
int maxFreq(string s, int maxLetters, int minSize, int maxSize)
5+
{
6+
char chs[256] = {};
7+
int nq = 0, res = 0, l = minSize;
8+
unordered_map<string, int> cnt;
9+
10+
for (int r = 0; r < s.size(); r++)
11+
{
12+
if (chs[s[r]]++ == 0) ++nq;
13+
if (l <= r)
14+
{
15+
if (--chs[s[r - l]] == 0) --nq;
16+
}
17+
18+
if (l - 1 <= r && nq <= maxLetters)
19+
{
20+
res = max(res, ++cnt[s.substr(r - l + 1, l)]);
21+
}
22+
}
23+
return res;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func maxFreq(s string, maxLetters int, minSize int, maxSize int) int {
2+
cnt := make(map[string]int)
3+
for i := 0; i <= len(s) - minSize; i++ {
4+
cnt[s[i:i + minSize]]++
5+
}
6+
7+
cntStr := make(map[int][]string)
8+
for k, v := range cnt {
9+
cntStr[v] = append(cntStr[v], k)
10+
}
11+
12+
keys := make([]int, 0)
13+
for k := range cntStr {
14+
keys = append(keys, k)
15+
}
16+
sort.Ints(keys)
17+
18+
for i := len(keys) - 1; i >= 0; i-- {
19+
for _, str := range cntStr[keys[i]] {
20+
sCnt := make(map[rune]int)
21+
nq := 0
22+
for _, c := range str {
23+
if sCnt[c] == 0 {
24+
nq++
25+
if nq > maxLetters {
26+
break
27+
}
28+
sCnt[c]++
29+
}
30+
}
31+
if nq <= maxLetters {
32+
return keys[i]
33+
}
34+
}
35+
}
36+
return 0
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
3+
char[] data = s.toCharArray();
4+
char[] chs = new char[256];
5+
int nq = 0, res = 0, l = minSize;
6+
Map<String, Integer> cnt = new HashMap();
7+
8+
for (int r = 0; r < data.length; r++) {
9+
if (chs[data[r]]++ == 0) ++nq;
10+
if (l <= r) {
11+
if (--chs[data[r - l]] == 0) nq--;
12+
}
13+
14+
if (l - 1 <= r && nq <= maxLetters) {
15+
String curStr = s.substring(r - l + 1, r + 1);
16+
int t = cnt.getOrDefault(curStr, 0) + 1;
17+
cnt.put(curStr, t);
18+
if (t > res) res = t;
19+
}
20+
}
21+
return res;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var maxFreq = function(s, maxLetters, minSize, maxSize) {
2+
let chs = new Array(256);
3+
chs.fill(0);
4+
let nq = res = 0, l = minSize, cnt = new Map();
5+
6+
for (let r = 0; r < s.length; r++) {
7+
if (chs[s.charCodeAt(r)]++ == 0) ++nq;
8+
if (l <= r) {
9+
if (--chs[s.charCodeAt(r - l)] == 0) --nq;
10+
}
11+
12+
if (l - 1 <= r && nq <= maxLetters) {
13+
let curStr = s.substring(r - l + 1, r + 1);
14+
let t = (cnt.get(curStr) || 0) + 1;
15+
cnt.set(curStr, t);
16+
if (t > res) res = t;
17+
}
18+
}
19+
return res;
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
3+
cnt = collections.Counter([s[i:i + minSize] for i in range(len(s) - minSize + 1)])
4+
for k, v in cnt.most_common():
5+
if len(set(k)) <= maxLetters:
6+
return v
7+
return 0

src/addProb.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Divide Array in Sets of K Consecutive Numbers"
6-
ID = 1296
7-
url = "https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/"
5+
name = "Maximum Number of Occurrences of a Substring"
6+
ID = 1297
7+
url = "https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/"
88
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

0 commit comments

Comments
 (0)