Skip to content

Commit 9ff52c2

Browse files
committed
feat: add solutions to lc problem: No.1297
No.1297.Maximum Number of Occurrences of a Substring
1 parent 2e2196e commit 9ff52c2

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,97 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:哈希表 + 枚举**
61+
62+
根据题目描述,如果一个长串满足条件,那么这个长串的子串(长度至少为 `minSize`)也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 `minSize` 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。
63+
64+
时间复杂度 $O(n \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 `minSize` 的大小。本题中 $m$ 不超过 $26$。
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**
6369

6470
<!-- 这里可写当前语言的特殊实现逻辑 -->
6571

6672
```python
67-
73+
class Solution:
74+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
75+
ans = 0
76+
cnt = Counter()
77+
for i in range(len(s) - minSize + 1):
78+
t = s[i: i + minSize]
79+
ss = set(t)
80+
if len(ss) <= maxLetters:
81+
cnt[t] += 1
82+
ans = max(ans, cnt[t])
83+
return ans
6884
```
6985

7086
### **Java**
7187

7288
<!-- 这里可写当前语言的特殊实现逻辑 -->
7389

7490
```java
91+
class Solution {
92+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
93+
int ans = 0;
94+
Map<String, Integer> cnt = new HashMap<>();
95+
for (int i = 0; i < s.length() - minSize + 1; ++i) {
96+
String t = s.substring(i, i + minSize);
97+
Set<Character> ss = new HashSet<>();
98+
for (int j = 0; j < minSize; ++j) {
99+
ss.add(t.charAt(j));
100+
}
101+
if (ss.size() <= maxLetters) {
102+
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
103+
ans = Math.max(ans, cnt.get(t));
104+
}
105+
}
106+
return ans;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
117+
int ans = 0;
118+
unordered_map<string, int> cnt;
119+
for (int i = 0; i < s.size() - minSize + 1; ++i) {
120+
string t = s.substr(i, minSize);
121+
unordered_set<char> ss(t.begin(), t.end());
122+
if (ss.size() <= maxLetters) {
123+
ans = max(ans, ++cnt[t]);
124+
}
125+
}
126+
return ans;
127+
}
128+
};
129+
```
75130
131+
### **Go**
132+
133+
```go
134+
func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
135+
cnt := map[string]int{}
136+
for i := 0; i < len(s)-minSize+1; i++ {
137+
t := s[i : i+minSize]
138+
ss := map[rune]bool{}
139+
for _, c := range t {
140+
ss[c] = true
141+
}
142+
if len(ss) <= maxLetters {
143+
cnt[t]++
144+
if ans < cnt[t] {
145+
ans = cnt[t]
146+
}
147+
}
148+
}
149+
return
150+
}
76151
```
77152

78153
### **...**

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,82 @@ It satisfies the conditions, 2 unique letters and size 3 (between minSize and ma
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
51+
ans = 0
52+
cnt = Counter()
53+
for i in range(len(s) - minSize + 1):
54+
t = s[i: i + minSize]
55+
ss = set(t)
56+
if len(ss) <= maxLetters:
57+
cnt[t] += 1
58+
ans = max(ans, cnt[t])
59+
return ans
5060
```
5161

5262
### **Java**
5363

5464
```java
65+
class Solution {
66+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
67+
int ans = 0;
68+
Map<String, Integer> cnt = new HashMap<>();
69+
for (int i = 0; i < s.length() - minSize + 1; ++i) {
70+
String t = s.substring(i, i + minSize);
71+
Set<Character> ss = new HashSet<>();
72+
for (int j = 0; j < minSize; ++j) {
73+
ss.add(t.charAt(j));
74+
}
75+
if (ss.size() <= maxLetters) {
76+
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
77+
ans = Math.max(ans, cnt.get(t));
78+
}
79+
}
80+
return ans;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
91+
int ans = 0;
92+
unordered_map<string, int> cnt;
93+
for (int i = 0; i < s.size() - minSize + 1; ++i) {
94+
string t = s.substr(i, minSize);
95+
unordered_set<char> ss(t.begin(), t.end());
96+
if (ss.size() <= maxLetters) {
97+
ans = max(ans, ++cnt[t]);
98+
}
99+
}
100+
return ans;
101+
}
102+
};
103+
```
55104
105+
### **Go**
106+
107+
```go
108+
func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
109+
cnt := map[string]int{}
110+
for i := 0; i < len(s)-minSize+1; i++ {
111+
t := s[i : i+minSize]
112+
ss := map[rune]bool{}
113+
for _, c := range t {
114+
ss[c] = true
115+
}
116+
if len(ss) <= maxLetters {
117+
cnt[t]++
118+
if ans < cnt[t] {
119+
ans = cnt[t]
120+
}
121+
}
122+
}
123+
return
124+
}
56125
```
57126

58127
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
4+
int ans = 0;
5+
unordered_map<string, int> cnt;
6+
for (int i = 0; i < s.size() - minSize + 1; ++i) {
7+
string t = s.substr(i, minSize);
8+
unordered_set<char> ss(t.begin(), t.end());
9+
if (ss.size() <= maxLetters) {
10+
ans = max(ans, ++cnt[t]);
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
2+
cnt := map[string]int{}
3+
for i := 0; i < len(s)-minSize+1; i++ {
4+
t := s[i : i+minSize]
5+
ss := map[rune]bool{}
6+
for _, c := range t {
7+
ss[c] = true
8+
}
9+
if len(ss) <= maxLetters {
10+
cnt[t]++
11+
if ans < cnt[t] {
12+
ans = cnt[t]
13+
}
14+
}
15+
}
16+
return
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
3+
int ans = 0;
4+
Map<String, Integer> cnt = new HashMap<>();
5+
for (int i = 0; i < s.length() - minSize + 1; ++i) {
6+
String t = s.substring(i, i + minSize);
7+
Set<Character> ss = new HashSet<>();
8+
for (int j = 0; j < minSize; ++j) {
9+
ss.add(t.charAt(j));
10+
}
11+
if (ss.size() <= maxLetters) {
12+
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
13+
ans = Math.max(ans, cnt.get(t));
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
3+
ans = 0
4+
cnt = Counter()
5+
for i in range(len(s) - minSize + 1):
6+
t = s[i: i + minSize]
7+
ss = set(t)
8+
if len(ss) <= maxLetters:
9+
cnt[t] += 1
10+
ans = max(ans, cnt[t])
11+
return ans

0 commit comments

Comments
 (0)