Skip to content

Commit d398d76

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 1297_Maximum_Number_of_Occurrences_of_a_Substring.java
1 parent c703da9 commit d398d76

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
3+
int result = 0;
4+
Map<Character, Integer> cMap = new HashMap<>();
5+
Map<String, Integer> sMap = new HashMap<>();
6+
7+
for (int i = 0, j = 0; j < s.length(); j++) {
8+
char c = s.charAt(j);
9+
cMap.put(c, cMap.getOrDefault(c, 0) + 1);
10+
11+
if (j - i + 1 > minSize) {
12+
char l = s.charAt(i);
13+
cMap.put(l, cMap.get(l) - 1);
14+
15+
if (cMap.get(l) == 0) {
16+
cMap.remove(l);
17+
}
18+
19+
++i;
20+
}
21+
22+
if (j - i + 1 >= minSize && cMap.size() <= maxLetters) {
23+
String str = s.substring(i, j + 1);
24+
sMap.put(str, sMap.getOrDefault(str, 0) + 1);
25+
result = Math.max(result, sMap.get(str));
26+
}
27+
}
28+
29+
return result;
30+
}
31+
}

0 commit comments

Comments
 (0)