inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cnt = Counter()
# for size in range(minSize, minSize+1):
# size = minSize
# for i in range(len(s)-size+1):
# ss = s[i:i+size]
# if len(set(ss)) <= maxLetters:
# cnt[ss]+=1
# return max(cnt.values()) if cnt else 0
# sliding window
res = collections.Counter()
size = minSize
while size <= maxSize:
for i in range(len(s)-size+1):
ss = s[i:i+size]
if len(set(ss)) <= maxLetters:
res[ss] += 1
size += 1
return max(res.values()) if res else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s, maxLetters, minSize, maxSize):
if minSize > len(s):
return 0
words = {}
for size in range(minSize, maxSize + 1):
left = 0
right = size - 1
while right < len(s):
word = s[left:right + 1]
if len(set(word)) <= maxLetters:
if word not in words:
words[word] = 0
words[word] += 1
left += 1
right += 1
maxOccurences = 0
for word in words:
maxOccurences = max(maxOccurences, words[word])
return maxOccurences
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# k = minSize
# count = collections.Counter(s[i:i + k] for i in range(len(s) - k + 1))
# return max([count[w] for w in count if len(set(w)) <= maxLetters] + [0])
cnt = Counter()
for size in range(minSize, maxSize+1):
for i in range(len(s)-size+1):
ss = s[i:i+size]
if len(set(ss)) <= maxLetters:
cnt[ss]+=1
return max(cnt.values()) if cnt else 0
# sliding window
# res = collections.Counter()
# n = len(s)
# size = minSize
# while size <= maxSize:
# M = collections.defaultdict(int)
# for i, c in enumerate(s):
# if i < size:
# M[c] += 1
# continue
# if len(M) <= maxLetters:
# res[s[i-size:i]] += 1
# # slide the window
# M[s[i-size]] -= 1
# if M[s[i-size]] == 0:
# del M[s[i-size]]
# M[c] += 1
# # check for last one
# if len(M) <= maxLetters:
# res[s[n-size:]] += 1
# size += 1
# # print(res.most_common(1))
# ans = res.most_common(1)
# if not ans:
# return 0
# else:
# return ans[0][1]
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
from collections import Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dic = defaultdict(int)
for i in range(0, len(s) - minSize + 1):
for j in range(minSize, maxSize + 1):
if i + j > len(s):
break
cur_str = s[i:i+j]
unique_letters = set(cur_str)
if len(unique_letters) > maxLetters:
continue
else:
dic[cur_str] += 1
res = 0
for s in dic:
res = max(res, dic[s])
return res
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
maxOccurrences = 0
substrings = dict()
for i in range(minSize, maxSize + 1):
for j in range(len(s)):
if i + j <= len(s):
current = s[j:j+i]
if len(set(current)) <= maxLetters:
if current in substrings:
substrings[current] += 1
else:
substrings[current] = 1
if substrings[current] > maxOccurrences:
maxOccurrences = substrings[current]
return maxOccurrences
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dict = {}
for i in range(len(s)):
for j in range(i+minSize, i+maxSize+1):
# check if substr already in dict
if j <= len(s):
substr = s[i:j]
if len(set(substr)) <= maxLetters:
if substr in dict:
dict[substr] += 1
else:
dict[substr] = 1
# count max value of dict
max_count = 0
for k,v in list(dict.items()):
max_count = max(max_count, v)
return max_count
# brute force - TLE
# max_count = 0
# for i in range(len(s)):
# for j in range(i+minSize, i+maxSize+1):
# if j < len(s):
# substr = s[i:j]
# # print(\"checking substr: \", substr)
# # valid substring length
# if len(set(substr)) <= maxLetters:
# # checking first condition
# # do the function
# cc = self.count(substr, s, maxLetters)
# max_count = max(max_count, cc)
# return max_count
# def count(self, substr, s, maxLetters):
# # print(\"\")
# # print(\"substr passed: \", substr)
# # return the countOcurrences
# count_times = 0
# j = len(substr)
# for i in range(len(s)-j+1):
# # print(\"comparing with s[i:i+j]: \", s[i:i+j])
# if s[i:i+j] == substr:
# count_times += 1
# return count_times
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if len(s)<minSize:
return 0
if minSize>maxSize:
return 0
res=0
d={}
for i in range(len(s)-minSize+1):
temp=s[i:i+minSize]
if len(set(temp))<=maxLetters:
d[temp]=d.get(temp,0)+1
res=max(res,d[temp])
return res
'''
res=0
for i in range(minSize,maxSize+1):
#print(\"a\")
r=[]
for j in range(i,len(s)+1):
#print(s[j-minSize:j])
a=s[j-minSize:j]
if len(set(list(a)))<=maxLetters:
#print(set(list(a)))
r.append(a)
#print(r)
if len(r)!=0:
r1 = max(set(r), key = r.count)
#print(r.count(r1))
r2=r.count(r1)
res=max(res,r2)
return res
'''
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = collections.defaultdict(int)
max_freq = 0
for win in range(minSize, maxSize+1):
for i in range(len(s) - win + 1):
sub_seq = s[i:i+win]
if len(set(sub_seq)) <= maxLetters:
freq[sub_seq] += 1
max_freq = max(max_freq, max(freq.values()) if freq else 0)
return max_freq
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
max_occ = 0
D = {}
for i in range(len(s)):
for j in range(i+minSize,i+maxSize+1):
if j <= len(s):
s_s = s[i:j]
else:
continue
# print(s_s)
# print(set(s_s))
if len(set(s_s)) <= maxLetters:
# print(s_s)
if s_s in D.keys():
D[s_s] +=1
else:
D[s_s] = 1
max_occ = max(D[s_s], max_occ)
return max_occ
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# sliding window, subtract left, if == 0, delete from dict (sliding caterpillar)
currDct = {}
ansDct = defaultdict(int)
length = len(s)
l = 0; r = 0
# init to min size; caterpillar sliding window
while l + minSize < length+1:
# expand to min
currDct = Counter(s[l:l+minSize])
if len(currDct) <= maxLetters:
ansDct[s[l:l+minSize]] += 1
else: l += 1; continue
# expand to maxSize
if minSize == maxSize: l += 1; continue
r = l + minSize + 1;
while r < length and r < l + maxSize:
# add to the right
currDct[s[r]] += 1
# del to the left
currDct[s[l]] -= 1
if currDct[s[l]] == 0: del currDct[s[l]]
if len(currDct) <= maxLetters:
ansDct[s[l:r+1]] += 1
r += 1
l += 1
# print(ansDct)
return 0 if not ansDct else max(ansDct.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import operator
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq=collections.defaultdict(int)
for size in range(minSize, maxSize + 1):
j = size
window = s[0:size]
while j <= len(s):
if len(set(window)) <= maxLetters:
freq[window] += 1
j += 1
window = s[j - size:j]
if freq:
return max(freq.values())
else:
return 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counter = dict()
n = len(s)
for i in range(n-minSize+1):
for j in range(i+minSize-1, i+maxSize):
if j>=n:
break
if len(set(s[i:j+1]))<=maxLetters:
counter[s[i:j+1]] = counter.get(s[i:j+1], 0) + 1
# print(counter)
if list(counter.values())==[]: return 0
return max(counter.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
d = {}
for i in range(n):
for j in range(minSize, maxSize+1):
if i+j> n:
break
if len(set(s[i:i+j])) <= maxLetters:
d[ s[i:i+j] ] = d.get(s[i:i+j],0) +1
# print(d)
return max(list(d.values()), default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
from collections import Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dic = defaultdict(int)
for i in range(0, len(s) - minSize + 1):
for j in range(minSize, maxSize + 1):
if i + j > len(s):
break
cur_str = s[i:i+j]
unique_letters = set(cur_str)
if len(unique_letters) > maxLetters:
continue
else:
dic[cur_str] += 1
res = 0
for s in dic:
res = max(res, dic[s])
return res
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import operator
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq=collections.defaultdict(int)
for size in range(minSize, maxSize + 1):
j = size
window = s[0:size]
while j <= len(s):
if len(set(window)) <= maxLetters:
freq[window] += 1
j += 1
window = s[j - size:j]
if freq:
return max(freq.items(), key=operator.itemgetter(1))[1]
else:
return 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substrings = {}
for i in range(len(s)-minSize+1):
sub = s[i:i+minSize]
if sub in substrings:
substrings[sub] += 1
else:
if len(Counter(sub)) <= maxLetters:
substrings[sub] = 1
if len(substrings):
return max(substrings.values())
else:
return 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = 0
ht = collections.Counter()
for i in range(len(s)):
for j in range(i+1,len(s)+1):
substring = s[i:j]
length = len(substring)
if length >= minSize and length <= maxSize and len(set(substring)) <= maxLetters:
ht[substring]+=1
elif length > maxSize:
break
return max(v for k,v in ht.items()) if ht else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substrings = dict()
for i in range(minSize - 1, maxSize):
for j in range(i,len(s)):
if len(set(s[j-i: j + 1])) <= maxLetters:
substrings[s[j-i: j + 1]] = substrings.get(s[j-i:j + 1], 0) + 1
return max(substrings.values()) if len(substrings) else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
seen = collections.defaultdict(int)
n = len(s)
for k in range(minSize,maxSize+1):
counts = collections.Counter(s[:k])
if len(counts) <= maxLetters:
seen[s[:k]] += 1
for i in range(n-k):
counts[s[i]] -= 1
if counts[s[i]] == 0:
del counts[s[i]]
counts[s[i+k]] += 1
if len(counts) <= maxLetters:
seen[s[i+1:i+k+1]] += 1
return max(seen.values()) if len(seen) > 0 else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substrings = collections.defaultdict(int)
for k in range(minSize,maxSize+1):
for i in range(len(s)-k+1):
substrings[s[i:i+k]]+=1
maxCount = 0
for k,v in list(substrings.items()):
if(len(set(k))<=maxLetters):
maxCount=max(maxCount,v)
return maxCount
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
occurence = {}
for i in range(len(s)):
for j in range(minSize,maxSize+1):
if i+j > len(s):
break
if len(set(s[i:i+j])) <= maxLetters:
if s[i:i+j] not in occurence:
occurence[s[i:i+j]] = 0
occurence[s[i:i+j]]+=1
# print(occurence)
if len(occurence) == 0:
return 0
return max(occurence.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
i=numUnique=0; N=len(s); count=collections.defaultdict(int); seen=collections.defaultdict(int)
ans=0
for i in range(N):
count=collections.defaultdict(int)
numUnique=0
power=1
hash=0
for j in range(i,min(i+26,N)):
count[s[j]]+=1
if count[s[j]]==1:
numUnique+=1
add=(ord(s[j])-ord('a')+1)*(27**power)
hash+=add
power+=1
if numUnique>maxLetters or j-i+1>maxSize:
break
if minSize<=j-i+1<=maxSize and numUnique<=maxLetters:
# have a valid subs
# print(s[i:j+1], hash)
seen[hash]+=1
ans=max(ans,seen[hash])
return ans
# for j in range(N):
# for j in range(N):
# if count[s[j]]==0:
# numUnique+=1
# count[s[j]]+=1
# while j-i+1>maxSize or numUnique>maxLetters:
# count[s[i]]-=1
# if count[s[i]]==0:
# numUnique-=1
# i+=1
# if j-i+1>=minSize:
# # we finally have a valid substring
# for k in range(i,j+1):
# curr=\"\"
# for l in range(k,j+1):
# curr+=s[l]
# if len(curr)>=minSize:
# seen[curr]+1=26
# subs=s[i:j+1]
# seen[subs]+=1
# ans=max(ans,seen[subs])
# print(seen)
# return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if len(s) < minSize:
return 0
res = 0
def is_good(ss):
return len(set(ss)) <= maxLetters
for sz in range(minSize, maxSize + 1):
cnt = collections.defaultdict(int)
for i in range(len(s) - sz + 1):
if is_good(s[i:i+sz]):
cnt[s[i:i+sz]] += 1
res = max(res, cnt[s[i:i+sz]])
#print(sz, cnt)
return res
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
store = collections.defaultdict(int)
for i in range(0, len(s) - minSize+ 1):
for j in range(0, maxSize-minSize + 1):
if (i+j+minSize) > len(s):
break
subS = s[i: i+ minSize + j]
checkSize = set(subS)
if len(checkSize) <= maxLetters:
store[subS] += 1
maxNum = 0
for key, val in store.items():
if val > maxNum:
maxNum = val
return maxNum
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = defaultdict(int)
for size in range(minSize, maxSize+1):
# key = [0]*26
q = deque()
for i in range(0, len(s)):
# if i < size-1:
# key[ord(s[i])-97] += 1
q.append(s[i])
if i > size-1:
q.popleft()
if (i >= size-1) and len(set(q)) <= maxLetters:
# print(tuple(key))
count[tuple(q)] += 1
if len(count) == 0:
return 0
return max(count.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
# O(n) time and space
# Only need minSize, if string S with size N > minSize has frequency F, then the substring of S must also have frequency F
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
ans = 0
str_freq = collections.Counter()
for i in range(len(s)-minSize+1):
candidate = s[i:i+minSize]
if len(set(candidate)) <= maxLetters:
str_freq[candidate] += 1
ans = max(ans, str_freq[candidate])
return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
dic = collections.defaultdict(int)
res = 0
for i in range(n):
for j in range(i+ minSize - 1,min(i+maxSize,n)):
temp = s[i:j+1]
if len(set(temp)) <= maxLetters:
dic[temp] += 1
res = max(res,dic[temp])
return res
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
l = len(s)
ans = 0
for i in range(minSize,maxSize+1):
mp=collections.defaultdict(int)
for j in range(l-i+1):
cc = collections.defaultdict(int)
sub = s[j:j+i]
if len(set(sub))<=maxLetters:
mp[sub]+=1
if len(mp.keys())>0:
ans=max(max(mp.values()),ans)
return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = defaultdict(int)
for size in range(minSize, maxSize+1):
for i in range(len(s)-size+1):
substring = s[i:i+size]
if len(set(substring)) <= maxLetters:
count[substring] += 1
if count:
return max(count.values())
return 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if not s or len(s) < minSize:
return 0
gMax = -1
while minSize <= maxSize:
start = 0
end = minSize - 1
freqMap = {}
lMax = 0
while end < len(s):
sub = s[start:end + 1]
# print(sub, self.checkUnique(sub))
if sub in freqMap or self.checkUnique(sub, maxLetters):
if sub not in freqMap:
freqMap[sub] = 1
else:
freqMap[sub] += 1
if freqMap[sub] > lMax:
lMax = freqMap[sub]
start += 1
end += 1
# print(lMax, gMax, minSize)
if lMax > gMax:
gMax = lMax
minSize += 1
return gMax
def checkUnique(self, string, maxLetters):
sett = set(string)
if len(sett) > maxLetters:
return False
else:
return True
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dict = {}
left, right = 0, minSize
while left < len(s):
while right - left <= maxSize and right <= len(s):
sub = s[left:right]
if sub in dict:
dict[sub] += 1
elif self.isUniqueAmount(sub, maxLetters):
dict[sub] = 1
right += 1
left += 1
right = left + minSize
retValue = 0
for k, v in list(dict.items()):
retValue = max(retValue, v)
return retValue
def isUniqueAmount(self, s: str, maxLetters: int) -> bool:
return len(set(s)) <= maxLetters
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
onetime = False
if minSize == maxSize:
onetime = True
valid_candidates = {}
maxoccurrences = 0
def check_candidates(test):
nonlocal maxoccurrences
if len(set(test)) <= maxLetters:
valid_candidates[test] = valid_candidates.get(test, 0) + 1
maxoccurrences = max(maxoccurrences, valid_candidates[test])
#find all possible substrings
for i in range(len(s)):
for j in range(minSize, maxSize+1):
if i + j <= len(s):
check_candidates(s[i:i+j])
return maxoccurrences
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dict = {}
left, right = 0, minSize
while left < len(s):
while right - left <= maxSize and right <= len(s):
sub = s[left:right]
if self.isUniqueAmount(sub, maxLetters):
self.addToDict(sub, dict)
right += 1
left += 1
right = left + minSize
retValue = 0
for k, v in list(dict.items()):
retValue = max(retValue, v)
return retValue
def addToDict(self, sub: str, dict: {}) -> None:
if sub not in dict:
dict[sub] = 0
dict[sub] += 1
def isUniqueAmount(self, s: str, maxLetters: int) -> bool:
return len(set(s)) <= maxLetters
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cnt = Counter()
for sz in range(minSize, min(maxSize, len(s)) + 1):
cur = Counter(s[:sz])
if len(cur) <= maxLetters:
cnt[s[:sz]] += 1
for i in range(1, len(s) - sz + 1):
# print(s[i:i+sz], s[i+sz-1])
cur[s[i+sz-1]] += 1
cur[s[i-1]] -= 1
if cur[s[i-1]] == 0:
cur.pop(s[i-1])
if len(cur) <= maxLetters:
cnt[s[i:i+sz]] += 1
return max(cnt.values()) if cnt else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
validStrings = {}
for currSize in range(minSize, maxSize+1):
self.getValidString(currSize, s, maxLetters, validStrings)
return self.getMaxCount(validStrings)
def getValidString(self,currSize, s, maxLetters, validStrings):
left = 0
right = 0
currWindow = {}
uniqueCounts = 0
for right in range(currSize):
uniqueCounts = self.insert(s[right],currWindow, uniqueCounts)
self.insertValidString(uniqueCounts,maxLetters, left, right, s, validStrings )
while right < len(s)-1:
uniqueCounts = self.insert(s[right+1],currWindow, uniqueCounts)
uniqueCounts = self.remove(s[left],currWindow, uniqueCounts)
left += 1
right += 1
self.insertValidString(uniqueCounts,maxLetters, left, right, s, validStrings )
def insert(self,char,currWindow, uniqueCounts):
if char not in currWindow:
currWindow[char] = 1
uniqueCounts += 1
else:
currWindow[char] += 1
return uniqueCounts
def remove(self,char,currWindow, uniqueCounts):
currWindow[char] -= 1
if currWindow[char] == 0:
del currWindow[char]
uniqueCounts -= 1
return uniqueCounts
def getMaxCount(self,validStrings):
maxCount = 0
for string in validStrings:
if validStrings[string] > maxCount:
maxCount = validStrings[string]
return maxCount
def insertValidString(self,uniqueCounts,maxLetters, left, right, s, validStrings ):
if uniqueCounts <= maxLetters:
currWord = s[left:right+1]
if currWord not in validStrings:
validStrings[currWord] = 1
else:
validStrings[currWord] += 1
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
substr = collections.Counter()
counter = [collections.Counter(s[:minSize])]
if len(counter[-1]) <= maxLetters:
substr[s[:minSize]] += 1
for i in range(1, maxSize - minSize + 1):
counter.append(collections.Counter(counter[-1]))
counter[-1][s[minSize + i - 1]] += 1
if len(counter[-1]) <= maxLetters:
substr[s[:minSize + i]] += 1
# print(counter)
# print(substr)
for i in range(minSize, n):
l = i - minSize
for j, cnt in enumerate(counter):
r = i + j
if r >= n:
break
cnt[s[l]] -= 1
cnt[s[r]] += 1
if cnt[s[l]] == 0:
cnt.pop(s[l])
if len(cnt) <= maxLetters:
substr[s[l + 1: r + 1]] += 1
# print(counter)
# print(substr)
return max(substr.values(), default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if len(s) == len(set(s)):
return 0
from collections import defaultdict
corpus = defaultdict(int)
for i in range(minSize, maxSize+1):
for j in range(len(s) - i + 1):
if len(set(s[j:j+i])) <= maxLetters:
corpus[s[j:j+i]] += 1
if len(corpus) == 0:
return 0
return max(corpus.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = 0
diff = maxSize-minSize
hashM = {}
maxC = float('-inf')
for r in range(len(s)-minSize+1):
for i in range(diff+1):
if r + minSize + i <= len(s):
if len(set(s[r:r+minSize+i])) <= maxLetters:
hashM[s[r:r+minSize+i]] = hashM.get(s[r:r+minSize+i], 0) + 1
maxC = max(maxC, hashM[s[r:r+minSize+i]])
return(maxC if maxC != float('-inf') else 0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counts = {} # string -> number
for current_size in range(minSize, maxSize + 1):
window = {}
for i in range(current_size):
c = s[i]
window[c] = window.get(c, 0) + 1
for i in range(current_size, len(s) + 1):
start = i - current_size
if len(window) <= maxLetters:
sub = s[start:i]
counts[sub] = counts.get(sub, 0) + 1
if i == len(s):
break
# add current
c = s[i]
window[c] = window.get(c, 0) + 1
# remove tail
c = s[start]
window[c] -= 1
if window[c] == 0:
del window[c]
# print(counts)
return max(counts.values()) if len(counts) else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
lookup = {}
for size in range(minSize, maxSize + 1):
for i in range(0, len(s) - size + 1):
sub_s = s[i:i+size]
if len(set(sub_s)) <= maxLetters:
if sub_s not in lookup:
lookup[sub_s] = 0
lookup[sub_s] += 1
if not lookup:
return 0
return max(lookup.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
start = 0
end = 0
sub = {}
result = {}
self.max_size = 0
def add_sub(pos):
if s[pos] in sub:
sub[s[pos]] +=1
else:
sub[s[pos]] = 1
def rem_sub(pos):
if s[pos] in sub:
if sub[s[pos]] == 1:
del sub[s[pos]]
else:
sub[s[pos]] -= 1
def add_res(string):
if string in result:
result[string] +=1
else:
result[string] = 1
self.max_size = max(self.max_size,result[string])
for size in range(minSize,maxSize+1):
while start <= len(s)-size:
if (end-start)+1 < size:
add_sub(end)
end +=1
else:
add_sub(end)
if len(sub) <= maxLetters:
add_res(s[start:end+1])
rem_sub(start)
start +=1
end +=1
start = 0
end = 0
sub = {}
return self.max_size
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
my_dict = {}
for i in range(len(s)-minSize+1):
if len(set(s[i:i+minSize]))<=maxLetters:
if s[i:i+minSize] in my_dict:
my_dict[s[i:i+minSize]] += 1
else:
my_dict[s[i:i+minSize]] = 1
# print(my_dict)
k = minSize+1
if minSize!=maxSize:
while(maxSize>=k):
for i in range(len(s)-k+1):
if len(set(s[i:i+k]))<=maxLetters:
if s[i:i+k] in my_dict:
my_dict[s[i:i+k]] += 1
else:
my_dict[s[i:i+k]] = 1
# print(my_dict)
k+=1
return max(my_dict.values()) if my_dict else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = Counter()
for k in range(minSize, maxSize + 1):
window = Counter(s[:k])
if len(window) <= maxLetters:
count[s[:k]] += 1
for i in range(k, len(s)):
window[s[i]] += 1
window[s[i - k]] -= 1
if window[s[i - k]] == 0:
del window[s[i - k]]
if len(window) <= maxLetters:
count[s[i - k + 1:i + 1]] += 1
return max(list(count.values()), default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
wordCounter = collections.defaultdict()
# loop (maxSize - minSize)+1 times
for i in range((maxSize - minSize)+1):
charCounter = collections.Counter(s[:minSize+i])
if len(charCounter) <= maxLetters:
wordCounter[s[:minSize+i]] = 1
for j in range(minSize+i, len(s)):
charCounter[s[j-minSize-i]] -= 1
if charCounter[s[j-minSize-i]] <= 0:
del charCounter[s[j-minSize-i]]
charCounter[s[j]] += 1
if len(charCounter) <= maxLetters:
if s[j-minSize-i+1:j+1] in wordCounter:
wordCounter[s[j-minSize-i+1:j+1]] += 1
else:
wordCounter[s[j-minSize-i+1:j+1]] = 1
maxTimes = 0
for subString in wordCounter:
maxTimes = max(wordCounter[subString], maxTimes)
return maxTimes
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
res_dict = collections.defaultdict(int)
for lidx in range(n-minSize+1):
lval = lidx+minSize
rval = min(n,lidx+maxSize+1)
for ridx in range(lval, 1+rval):
counts = collections.Counter(s[lidx:ridx])
if len(counts) <= maxLetters:
res_dict[s[lidx:ridx]] += 1
else:
break
return max(res_dict.values()) if res_dict else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# for each substring of length, find unique chars
maxC = Counter()
ans = 0
for sz in range(minSize, maxSize+1):
unq = Counter()
for i in range(sz):
unq[s[i]] += 1
if (len(unq) <= maxLetters):
maxC[s[0:sz]] += 1
if(maxC[s[0:sz]] > ans): ans=maxC[s[0:sz]]
i, j = 0, sz - 1
while (j < len(s) - 1):
unq[s[i]] -= 1
if (unq[s[i]] == 0): del unq[s[i]]
i+=1
j+=1
unq[s[j]] += 1
if (len(unq) <= maxLetters):
maxC[s[i:j+1]] += 1
if(maxC[s[i:j+1]] > ans): ans=maxC[s[i:j+1]]
return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
subset_d = {}
for i in range(minSize, maxSize + 1):
for j in range(0, len(s) - i + 1):
substr = s[j:j + i]
subset_d[substr] = subset_d.get(substr, 0) + 1
max_occur = 0
for substr, val in list(subset_d.items()):
temp_s = set(list(substr))
if val > max_occur and len(temp_s) <= maxLetters:
max_occur = val
return max_occur
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
lenS = len(s)
#for counting uniq chars :
uniqSubStrDict = {}
#for i,char in enumerate(s):
# uniqDict[char] = i
count = 0
# edge casses:
# main alg.
Max = 0;
for i in range(minSize,maxSize + 1):
for j in range(lenS - i + 1) : # maxSize):
tstSubStr = s[j : j + i]
uniqChars = {}
for k in tstSubStr:
uniqChars[k] = 1 #uniqChars.get(k,0) + 1
if len(uniqChars) > maxLetters:
#print(' in over maxLetters loop -- tstSubStr = {} '.format(tstSubStr))
continue
#print(' tstSubStr = {} '.format(tstSubStr))
uniqSubStrDict[tstSubStr] = uniqSubStrDict.get(tstSubStr,0) + 1
#print(' dict count = {} '.format(uniqSubStrDict[tstSubStr]))
if uniqSubStrDict[tstSubStr] > Max :
Max = uniqSubStrDict[tstSubStr]
count = Max
#count = uniqSubStrDict[tstSubStr]
return count
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dict = {}
left, right = 0, minSize
while left < len(s):
while right - left <= maxSize and right <= len(s):
sub = s[left:right]
if self.isSize(sub, minSize, maxSize) and self.isUniqueAmount(sub, maxLetters):
self.addToDict(sub, dict)
right += 1
left += 1
right = left + minSize
retValue = 0
for k, v in list(dict.items()):
retValue = max(retValue, v)
return retValue
def addToDict(self, sub: str, dict: {}) -> None:
if sub not in dict:
dict[sub] = 0
dict[sub] += 1
def isSize(self, s: str, minSize: int, maxSize: int) -> bool:
length = len(s)
return minSize <= length and maxSize >= length
def isUniqueAmount(self, s: str, maxLetters: int) -> bool:
return len(set(s)) <= maxLetters
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
newDict = {}
for j in range(len(s) - minSize + 1):
word = s[j:j+minSize]
if word in newDict:
newDict[word] += 1
else:
if len(collections.Counter(word)) <= maxLetters:
newDict[word] = 1
return max(newDict.values()) if len(newDict) != 0 else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = {}
for i in range(0, len(s)-minSize+1):
sub = s[i:i+minSize]
chars = set()
for c in sub:
chars.add(c)
if len(chars)<=maxLetters:
if sub not in freq: freq[sub] = 0
freq[sub] += 1
best = 0
for sub in freq:
if freq[sub] > best:
best = freq[sub]
return best
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
#close the window, + 1
hashmap = {}
occ = {}
for i in range(len(s)):
hashmap[s[i]] = hashmap.get(s[i], 0) + 1
if i>=minSize:
#remove the first
hashmap[s[i-minSize]] -=1
if hashmap[s[i-minSize]] == 0:
del hashmap[s[i-minSize]]
if i >= minSize -1:
if len(hashmap) <= maxLetters:
substring = s[i-minSize+1: i+1]
occ[substring] = occ.get(substring, 0) + 1
if len(occ) == 0:
return 0
return max(occ.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
candidates = collections.Counter()
for i in range(len(s)-minSize+1):
if len(set(list(s[i:i+minSize])))<=maxLetters:
candidates[s[i:i+minSize]] += 1
return max(list(candidates.values())+[0])
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = collections.Counter(s[i:i+minSize] for i in range(len(s)-minSize+1))
return max([count[w] for w in count if len(set(w)) <= maxLetters] +[0])
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counter = collections.defaultdict(int)
mapping = collections.defaultdict(int)
if len(s) < minSize:
return 0
count = 0
for i in range(minSize):
mapping[s[i]] += 1
if mapping[s[i]] == 1:
count += 1
if count <= maxLetters:
counter[s[0:minSize]] += 1
for i in range(1, len(s) - minSize+1):
mapping[s[i-1]] -= 1
if mapping[s[i-1]] == 0:
count -= 1
mapping[s[i+minSize-1]] += 1
if mapping[s[i+minSize-1]] == 1:
count += 1
# print(s[i:i+minSize])
if count <= maxLetters:
counter[s[i:i+minSize]] += 1
# print(counter)
if not counter:
return 0
return max(counter.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s, maxLetters, minSize, maxSize):
result = 0
subStringFreq = collections.defaultdict(int)
window = collections.defaultdict(int)
low = 0
high = 0
while high < len(s):
window[s[high]] += 1
if (high - low + 1) == minSize:
if len(window) <= maxLetters:
subStringFreq[s[low:high+1]] += 1
result = max(result, subStringFreq[s[low:high+1]])
window[s[low]] -= 1
if window[s[low]] == 0:
del window[s[low]]
low += 1
high += 1
return result
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
x=defaultdict(int)
for i in range(len(s)-minSize+1):
sub=s[i:i+minSize]
if len(set(sub))<=maxLetters:
x[sub]+=1
return max(x.values(),default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
left = 0
result = 0
count = collections.defaultdict(int)
occurances = collections.defaultdict(int)
for right, char in enumerate(s):
count[char] += 1
while(right - left + 1 > minSize):
count[s[left]] -= 1
if count[s[left]] == 0:
del count[s[left]]
left += 1
if right - left + 1 == minSize and len(count) <= maxLetters:
occurances[s[left:right+1]] += 1
result = max(result, occurances[s[left:right+1]])
return result
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = collections.defaultdict(int)
for i in range(len(s) - minSize + 1):
freq[s[i:i + minSize]] += 1
mx = 0
for key in freq:
if len(set(key)) <= maxLetters and mx < freq[key]:
mx = freq[key]
return mx
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
l = 0
r = 0
strMap = collections.defaultdict(int)
curr = ''
currMap = collections.defaultdict(int)
while r < len(s):
char = s[r]
currMap[char] += 1
curr += char
while len(currMap) > maxLetters or len(curr) > minSize:
curr = curr[1:]
currMap[s[l]] -= 1
if currMap[s[l]] == 0:
del currMap[s[l]]
l += 1
if len(curr) == minSize:
strMap[curr] += 1
r += 1
return max(strMap.values()) if strMap else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
subCt = defaultdict(int)
currS = defaultdict(int)
mx = 0
l = 0
r = 0
while l < len(s) - minSize:
length = r - l + 1
if length > minSize:
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
# c = (r == len(s) - 1) and (l < len(s) - minSize)
# if c:
# currS[s[l]] -= 1
# if currS[s[l]] <= 0:
# del currS[s[l]]
# l += 1
currC = s[r]
currS[currC] += 1
condition = (r - l + 1) >= minSize and len(list(currS.keys())) <= maxLetters
if condition:
sub = s[l:r+1]
subCt[sub] += 1
if subCt[sub] > mx:
mx = subCt[sub]
if r < len(s) - 1:
r += 1
return mx
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
subCt = defaultdict(int)
currS = defaultdict(int)
mx = 0
l = 0
r = 0
while l < len(s) - minSize:
length = r - l + 1
if length > minSize:
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
currC = s[r]
currS[currC] += 1
condition = (r - l + 1) >= minSize and len(list(currS.keys())) <= maxLetters
if condition:
sub = s[l:r+1]
subCt[sub] += 1
if subCt[sub] > mx:
mx = subCt[sub]
if r < len(s) - 1:
r += 1
return mx
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if not s:
return 0
n = len(s)
substring_count = defaultdict(int)
memo_char = defaultdict(int)
reader = 0
writer = 0
while reader < len(s):
ch = s[reader]
memo_char[ch] += 1
window_len = reader - writer +1
while len(memo_char) > maxLetters or window_len > minSize:
wch = s[writer]
memo_char[wch] -= 1
if memo_char[wch] == 0:
del memo_char[wch]
writer += 1
window_len = reader - writer +1
if window_len >= minSize and window_len <= maxSize:
substring_count[tuple(s[writer:reader+1])] += 1
#print (s[writer:reader+1], substring_count, window_len)
reader += 1
if not substring_count:
return 0
return max(substring_count.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
subCt = defaultdict(int)
currS = defaultdict(int)
mx = 0
l = 0
r = 0
while l < len(s) - minSize:
length = r - l + 1
if length > minSize:
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
c = (r == len(s) - 1) and (l < len(s) - minSize)
if c:
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
currC = s[r]
currS[currC] += 1
condition = (r - l + 1) >= minSize and len(list(currS.keys())) <= maxLetters
if condition:
sub = s[l:r+1]
subCt[sub] += 1
if subCt[sub] > mx:
mx = subCt[sub]
if r < len(s) - 1:
r += 1
return mx
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
l = 0
r = 0
strMap = collections.defaultdict(int)
curr = ''
currMap = collections.defaultdict(int)
unique = 0
while r < len(s):
char = s[r]
if currMap[char] == 0:
unique += 1
currMap[char] += 1
curr += char
while unique > maxLetters or len(curr) > minSize:
curr = curr[1:]
currMap[s[l]] -= 1
if currMap[s[l]] == 0:
unique -= 1
l += 1
print(curr)
if len(curr) >= minSize:
strMap[curr] += 1
r += 1
return max(list(strMap.values()) or (0,0))
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dic = {}
for i in range(len(s)-minSize+1):
s1 = s[i:i+minSize]
unique = {}
for c in s1:
if c not in unique:
unique[c] = 1
if len(unique) <= maxLetters:
if s1 in dic:
dic[s1] += 1
else:
dic[s1] = 1
if i != len(s)-minSize and minSize != maxSize:
s2 = s[i:i+maxSize]
if s2[-1] not in unique:
unique[s2[-1]] = 1
if len(unique) <= maxLetters:
if s2 in dic:
dic[s2] += 1
else:
dic[s2] = 1
max_occr = 0
for key in dic:
if dic[key] > max_occr:
max_occr = dic[key]
return max_occr
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# actually don't need to go up to maxSize since if
# a string of maxSize has an occurence, then any substring
# has at least an equal number of occurences
seen = collections.defaultdict(int)
n = len(s)
counts = collections.Counter(s[:minSize])
if len(counts) <= maxLetters:
seen[s[:minSize]] += 1
for i in range(n-minSize):
counts[s[i]] -= 1
if counts[s[i]] == 0:
del counts[s[i]]
counts[s[i+minSize]] += 1
if len(counts) <= maxLetters:
seen[s[i+1:i+minSize+1]] += 1
return max(seen.values()) if len(seen) > 0 else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
strcount = Counter()
unique = set(s[:minSize])
front = 0
back = minSize
while back < len(s):
if len(unique) <= maxLetters:
strcount[s[front:back]] = strcount.get(s[front:back], 0) + 1
front+=1
back+=1
unique = set(s[front:back])
if len(unique) <= maxLetters:
strcount[s[front : back]] = strcount.get(s[front : back], 0) + 1
return max(list(strcount.values()) or [0])
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = defaultdict(int)
d = defaultdict(int)
max_freq = 0
for idx, a in enumerate(s):
count[a] += 1
if idx > minSize - 1:
last = s[idx - minSize]
count[last] -= 1
if count[last] == 0:
del count[last]
if idx >= minSize - 1:
if len(count) <= maxLetters:
d[s[idx-minSize+1: idx+1]] += 1
max_freq = max(max_freq, d[s[idx-minSize+1: idx+1]])
return max_freq
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = 0
left = 0
right = minSize-1
d = {}
length = len(s)
unique_let = {}
def popLetter(letter, n):
unique_let[letter] -= 1
if unique_let[letter] == 0:
n -= 1
return n
def addLetter(letter, n):
if unique_let.get(letter, 0) == 0:
n += 1
unique_let[letter] = unique_let.get(letter,0) + 1
return n
for letter in s[:minSize-1]:
n = addLetter(letter, n)
print(n)
print(unique_let)
while right < length:
if left > 0:
n = popLetter(s[left-1], n)
n = addLetter(s[right], n)
print(n)
if n <= maxLetters:
d[s[left:right+1]] = d.get(s[left:right+1], 0) + 1
right += 1
left += 1
print(d)
if not d.values():
return 0
return max(d.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import operator
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
lp = 0
rp = lp + minSize
perm_dict= {}
for i in range(len(s)-minSize+1):
temp = s[lp:rp]
if len(set(temp))<=maxLetters:
if temp not in list(perm_dict.keys()):
perm_dict[temp]=1
else:
perm_dict[temp]+=1
i+=1
lp+=1
rp+=1
print(perm_dict)
perm_dict = dict(sorted(list(perm_dict.items()), key=operator.itemgetter(1),reverse=True))
if len(list(perm_dict.keys()))>0:
return perm_dict[list(perm_dict.keys())[0]]
return 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
n = len(s)
s0 = s[:minSize]
counter = collections.Counter(s0)
substr = collections.Counter()
if len(counter) <= maxLetters:
substr[s0] += 1
for i in range(minSize, n):
l = i - minSize
counter[s[i - minSize]] -= 1
counter[s[i]] += 1
if counter[s[i - minSize]] == 0:
counter.pop(s[i - minSize])
if len(counter) <= maxLetters:
substr[s[i - minSize + 1: i + 1]] += 1
return max(substr.values(), default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, S, maxchars, minsize, maxsize):
n=len(S)
freq=Counter()
chars=Counter()
i=0
for j in range(n):
if j-i+1>minsize:
chars[S[i]]-=1
if chars[S[i]]==0:
del chars[S[i]]
i+=1
chars[S[j]]+=1
if j-i+1>=minsize:
if len(chars)<=maxchars:
freq[S[i:j+1]]+=1
return max(freq.values(),default=0)
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
i=0
j=0
map1={}
res={}
while j<len(s):
map1[s[j]]=map1.get(s[j],0)+1
if len(map1.keys())>maxLetters:
map1[s[i]]=map1[s[i]]-1
if map1[s[i]]==0:
del map1[s[i]]
i+=1
while len(map1.keys())<=maxLetters and j-i+1<=maxSize and j-i+1>=minSize:
#add to result first
res[s[i:j+1]]=res.get(s[i:j+1],0)+1
#remove the existing s[i]'s value from map
map1[s[i]]=map1[s[i]]-1
if map1[s[i]]==0:
del map1[s[i]]
#move i=i+1
i=i+1
j=j+1
if len(res)==0:
return 0
else:
maximum = max(res, key=res.get)
return res[maximum]
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
subCt = defaultdict(int)
currS = defaultdict(int)
mx = 0
l = 0
r = 0
while l < len(s) - minSize:
length = r - l + 1
if length > minSize:
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
c = (r == len(s) - 1) and (l < len(s) - minSize)
if c:
print('eer')
currS[s[l]] -= 1
if currS[s[l]] <= 0:
del currS[s[l]]
l += 1
currC = s[r]
currS[currC] += 1
condition = (r - l + 1) >= minSize and len(list(currS.keys())) <= maxLetters
if condition:
sub = s[l:r+1]
print(sub)
subCt[sub] += 1
if subCt[sub] > mx:
mx = subCt[sub]
if r < len(s) - 1:
r += 1
return mx
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if len(s) == 0:
return 0
rolling_hash = 0
letter_counts = collections.Counter()
hash_counts = collections.Counter()
unique_letters = set()
n = len(s)
for i in range(n):
ch = s[i]
rolling_hash = rolling_hash * 26 + ord(ch)
letter_counts[ch] += 1
unique_letters.add(ch)
if i + 1 < minSize:
continue
if len(unique_letters) <= maxLetters:
hash_counts[rolling_hash] += 1
remove_letter = s[i - minSize + 1]
rolling_hash -= (ord(remove_letter)) * 26**(minSize-1)
letter_counts[remove_letter] -= 1
if letter_counts[remove_letter] == 0:
unique_letters.remove(remove_letter)
return max(hash_counts.values()) if len(hash_counts) else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = collections.Counter(s[i:i + minSize] for i in range(len(s) - minSize + 1))
return max([count[w] for w in count if len(set(w)) <= maxLetters] + [0])
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
window_start = 0
window_letters = Counter()
substring_counts = Counter()
for window_end in range(len(s)):
window_letters[s[window_end]] += 1
substring_len = window_end-window_start+1
while substring_len > maxSize or len(window_letters) > maxLetters:
start_char = s[window_start]
window_letters[start_char] -= 1
if window_letters[start_char] == 0:
del window_letters[start_char]
window_start += 1
substring_len = window_end-window_start+1
while substring_len >= minSize:
assert substring_len <= maxSize
substring = s[window_start:window_end+1]
substring_counts[substring] += 1
start_char = s[window_start]
window_letters[start_char] -= 1
if window_letters[start_char] == 0:
del window_letters[start_char]
window_start += 1
substring_len = window_end-window_start+1
print(substring_counts)
if not substring_counts:
return 0
return max(substring_counts.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# for each substring of length, find unique chars
maxC = Counter()
ans = 0
sz = minSize
unq = Counter()
for i in range(sz):
unq[s[i]] += 1
if (len(unq) <= maxLetters):
maxC[s[0:sz]] += 1
if(maxC[s[0:sz]] > ans): ans=maxC[s[0:sz]]
i, j = 0, sz - 1
while (j < len(s) - 1):
unq[s[i]] -= 1
if (unq[s[i]] == 0): del unq[s[i]]
i+=1
j+=1
unq[s[j]] += 1
if (len(unq) <= maxLetters):
maxC[s[i:j+1]] += 1
if(maxC[s[i:j+1]] > ans): ans=maxC[s[i:j+1]]
return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxy: int, m: int, mm: int) -> int:
i,j = 0,0
count = collections.Counter()
count[s[0]]+=1
ans = collections.Counter()
u, n = 1, len(s)
key = s[0]
while True:
if u<=maxy and m<=i-j+1<=mm:
ans[key]+=1
if j<i and i-j+1>=m:
count[s[j]]-=1
if count[s[j]]==0: u-=1
j+=1
key = key[1:]
else:
i+=1
if i == n: break
if count[s[i]]==0: u+=1
count[s[i]]+=1
key+=s[i]
return max(ans.values()) if ans else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxy: int, m: int, mm: int) -> int:
i,j = 0,0
count = collections.Counter()
count[s[0]]+=1
ans = collections.Counter()
u = 1
n = len(s)
while True:
# print(f'j-{j}, i-{i}, u-{u}, ans-{ans}')
if u<=maxy and m<=i-j+1<=mm:
# print('Found!-',j,i,s[j:i+1])
ans[s[j:i+1]]+=1
if j<i and i-j+1>=m:
count[s[j]]-=1
if count[s[j]]==0: u-=1
j+=1
else:
i+=1
if i == n: break
if count[s[i]]==0: u+=1
count[s[i]]+=1
print(ans)
return max(ans.values()) if ans else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if not s or len(s) < minSize: return 0
res = defaultdict(int)
res[''] = 0
temp = defaultdict(int)
i = 0
j = minSize-1
for k in range(minSize):
temp[s[k]] += 1
if len(temp.keys()) <= maxLetters:
res[s[i:j+1]] += 1
while i < len(s)-minSize:
temp[s[i]] -= 1
i += 1
j += 1
temp[s[j]] += 1
if len([l for l in temp if temp[l]]) <= maxLetters:
res[s[i:j+1]] += 1
return max(res.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
from collections import defaultdict, Counter
d = defaultdict(int)
for i in range(len(s)-minSize+1):
t = s[i:i+minSize]
if len(Counter(t)) <= maxLetters:
d[t] += 1
return max(d.values()) if d else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import Counter, defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
d = defaultdict(int)
for i in range(len(s)-minSize+1):
sub = s[i:i+minSize]
if len(Counter(sub)) <= maxLetters:
d[sub] += 1
return max(d.values()) if len(list(d.values())) else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = {}
for i in range(len(s) - minSize + 1):
word = s[i: i+minSize]
if word in count:
count[word] += 1
else:
if len(set(word)) <= maxLetters:
count[word] = 1
return max(count.values()) if count else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
i = 0
j = 0
letterCounts = Counter()
substrCounts = Counter()
while j < len(s):
letterCounts[s[j]] += 1
while len(letterCounts) > maxLetters or j - i + 1 > minSize:
letterCounts[s[i]] -= 1
if letterCounts[s[i]] == 0:
del letterCounts[s[i]]
i += 1
if j - i + 1 == minSize:
substrCounts[s[i:j + 1]] += 1
j += 1
return 0 if len(substrCounts) == 0 else max(substrCounts.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
'''
uniq<=max
len(sub)>=min and <=max
'''
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
hash,w_hash,res={},{},float('-inf')
self.initial_fill(s,minSize,hash)
for i in range(0,len(s)-minSize+1):
if len(hash)<=maxLetters:
string=s[i:i+minSize]
w_hash[string]=1 if string not in w_hash else w_hash[string]+1
res=max(res,w_hash[string])
char=s[i]
if hash[char]==1: del hash[char]
else: hash[char]-=1
if i+minSize<len(s):
char=s[i+minSize]
hash[char]=1 if char not in hash else hash[char]+1
return res if res!=float('-inf') else 0
def initial_fill(self,s,Min,hash):
for i in range(0,Min):
char=s[i]
hash[char]=1 if char not in hash else hash[char]+1
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
left=0
right=0
_dict=defaultdict(int)
_dict2=defaultdict(int)
while right<len(s):
_dict[s[right]]+=1
while len(_dict)>maxLetters or right-left+1>maxSize or right-left+1>minSize:
_dict[s[left]]-=1
if _dict[s[left]]==0:
del(_dict[s[left]])
left+=1
if len(_dict)<=maxLetters and minSize<=right-left+1<=maxSize :
_dict2[s[left:right+1]]+=1
right+=1
if len(_dict2)==0:
return 0
return max(_dict2.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
dic = collections.defaultdict(int)
for i in range(len(s) - minSize + 1):
if len(collections.Counter(s[i:i + minSize])) <= maxLetters:
dic[s[i:i + minSize]] += 1
if not dic:
return 0
return max(dic.values())
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict, Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
def hasUnique(s):
dic = Counter(s)
if(len(dic) <= maxLetters):
return True
return False
def checkSubStrings(s):
dic = defaultdict(int)
maximum = 0
for i in range(0, len(s) - minSize + 1):
end = i + minSize
strr = s[i:end]
if(hasUnique(strr)):
dic[strr] += 1
maximum = max(maximum, dic[strr])
return maximum
return(checkSubStrings(s))
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cnt = defaultdict(int)
freq = defaultdict(int)
res, left = 0, 0
for i in range(len(s)):
cnt[s[i]] = cnt.get(s[i], 0) + 1
while len(cnt) > maxLetters or i - left + 1 > minSize:
cnt[s[left]] -= 1
if cnt[s[left]] == 0:
del cnt[s[left]]
left += 1
if (i - left + 1 <= maxSize and i - left + 1 >= minSize):
sub = s[left: i + 1]
freq[sub] = freq.get(sub, 0) + 1
return max(freq.values()) if freq else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
memo_dict = {}
for i in range(len(s)):
for j in range(i,len(s) + 1):
if j - i < minSize:
continue
if j - i > minSize:
break
temp = []
temp_str = s[i:j]
for char in temp_str:
temp.append(char)
if len(set(temp)) > maxLetters:
continue
if temp_str in memo_dict:
memo_dict[temp_str] += 1
else:
memo_dict[temp_str] = 1
if len(memo_dict) == 0: return 0
res = sorted(memo_dict, key = lambda x : memo_dict[x])
#print(res)
return memo_dict[res[-1]]
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counterSubstring = collections.defaultdict(int)
best = 0
for start in range(len(s)):
#for size in range(minSize, maxSize + 1):
if start + minSize <= len(s):
substring = s[start:start + minSize]
counterSubstring[substring] += 1
for substring in counterSubstring:
if len(set(substring)) <= maxLetters:
best = max(best, counterSubstring[substring])
return best
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = Counter()
for leftInd, char in enumerate(s):
seen = set([])
for rightInd in range(leftInd, leftInd + minSize):
if rightInd > len(s) - 1:
break
seen.add(s[rightInd])
if len(seen) > maxLetters:
break
if rightInd - leftInd + 1 >= minSize:
freq[s[leftInd:rightInd + 1]] += 1
ret = 0
for key, val in freq.items():
ret = max(ret, val)
return ret
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
max_occur = 0
freq_hash = {}
for i in range(len(s) - minSize + 1):
stri = s[i: i + minSize]
if len(collections.Counter(stri)) <= maxLetters:
if stri in freq_hash:
freq_hash[stri] += 1
else:
freq_hash[stri] = 1
max_occur = max(max_occur, freq_hash[stri])
return max_occur
# counts = dict()
# for j in range(len(s)-minSize+1):
# word = s[j:j+minSize]
# if word in counts:
# counts[word]+=1
# else:
# if len(collections.Counter(word))<=maxLetters:
# counts[word]=1
# return max(counts.values()) if len(counts)!=0 else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
ans = 0
l = minSize
counts = {}
for i in range(len(s) - l + 1):
string = s[i:i+l]
c = collections.Counter(string)
if len(c) <= maxLetters:
counts[string] = counts.get(string, 0) + 1
if counts:
ans = max(ans, max(counts.values()))
return ans
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if len(s) < minSize or maxSize == 0:
return 0
start = 0
end = start + minSize
count = Counter()
while end <= len(s):
unique = Counter(s[start:end])
if len(unique) <= maxLetters:
count[s[start:end]] += 1
start += 1
end += 1
return max(count.values()) if count else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
from collections import Counter
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# init first window
counts = defaultdict(int)
substrs = defaultdict(int)
start = 0
# expand window until we can't any more
for end in range(len(s)):
counts[s[end]] += 1
if end - start + 1 > minSize:
counts[s[start]] -= 1
if counts[s[start]] == 0:
del counts[s[start]]
start += 1
print (counts)
if end - start + 1 == minSize and len(counts) <= maxLetters:
substrs[s[start:end+1]] += 1
print (substrs)
if not substrs:
return 0
return max(substrs.values())
# decrement window until we meet the requirement
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counter=defaultdict(int)
for i in range(len(s)):
string=s[i:i+minSize]
if len(Counter(string))<=maxLetters and len(string)>=minSize:
counter[string]+=1
return max(counter.values()) if counter else 0
|
Given a string s, return the maximum number of ocurrences of any substring under the following rules:
The number of unique characters in the substring must be less than or equal to maxLetters.
The substring size must be between minSize and maxSize inclusive.
Example 1:
Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
Example 2:
Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
Example 3:
Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3
Example 4:
Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0
Constraints:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s only contains lowercase English letters.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# hash table to remember substrings
substrings = defaultdict(int)
char_counts = Counter(s[0:minSize-1])
start = 0
while start <= len(s) - minSize:
end = start + minSize - 1
end_char = s[end]
char_counts[end_char] += 1
unique_chars = len(char_counts)
if unique_chars <= maxLetters:
substrings[s[start:end+1]] += 1
start_char = s[start]
if char_counts[start_char] == 1:
del char_counts[start_char]
else:
char_counts[start_char] -= 1
start += 1
maxSubstrings = 0
for substring in substrings:
if substrings[substring] > maxSubstrings:
maxSubstrings = substrings[substring]
return maxSubstrings
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.