Maximum Number of Occurrences of a Substring LeetCode solution in Java

1
class Solution {
2
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
3
Map<String, Integer> count = new HashMap<>();
4
int ans = 0;
5
int n = s.length();
6

7
for (int i = 0; i + minSize - 1 < n; i++) {
8
String y = s.substring(i, i + minSize);
9
count.put(y, count.getOrDefault(y, 0) + 1);
10

11
int unique = uniqueCharactersInString(y);
12
if (unique <= maxLetters) {
13
ans = Math.max(ans, count.get(y));
14
}
15
}
16
// System.out.println(count);
17
return ans;
18
}
19

20
int uniqueCharactersInString(String y) {
21
Set<Character> hs = new HashSet<>();
22
for (int j = 0; j < y.length(); j++) hs.add(y.charAt(j));
23
return hs.size();
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0