inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
return pow(a, int(''.join(map(str, b))), 1337)
# from functools import reduce
# k=reduce(lambda x,y:x*10+y, b)
# print(k)
# return (a%1337)**k
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
# return pow(a, int(''.join(map(str, b))), 1337)
from functools import reduce
k=reduce(lambda x,y:x*10+y, b)
# print(k)
# return pow(a,k)%1337
return pow(a, k, 1337)
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
num = 0
currentpow = 1
for item in b[::-1]:
num += currentpow * item
currentpow *= 10
return self.binpow(a, num % 570)
def binpow(self, n, p):
if(p == 0):
return 1
if(p % 2 == 0):
return (self.binpow(n * n, p // 2)) % 1337
return (n * self.binpow(n, p - 1)) % 1337
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
a %= 1337
if len(b) == 1:
return self.powMod(a, b[0])
return self.powMod(self.superPow(a, b[:-1]), 10) * self.powMod(a, b[-1]) % 1337
"""
res = 1
x = a % 1337
for y in b[::-1]:
res = (res * (x ** y)) % 1337
x = (x ** 10) % 1337
return res
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
result = 1
fermatb = (int(''.join(map(str, b)))) % 570
while fermatb:
if fermatb & 1:
result = (result * a) % 1337
a = (a * a) % 1337
fermatb >>= 1
return result
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
result = 1
x = a % 1337
for y in b[::-1]:
result = (result * (x**y)) % 1337
x = (x**10) % 1337
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:
n = len(s)
count = collections.Counter(s[i : i + minSize] for i in range(0, n - minSize + 1))
res = 0
for k, v in count.items():
if len(set(k)) <= maxLetters:
res = max(res, v)
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:
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])
|
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 = Counter(s[i:i + k] for i in range(len(s) - k + 1))
print(count)
ans=0
for w in count:
if len(set(w)) <= maxLetters:
ans=max(count[w],ans)
return ans
#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:
word_dic = {}
str_dic = {}
r_end = 0
ans = 0
def update_ans(length):
if length >= minSize:
return min(length,maxSize) - minSize + 1
return 0
for i in range(len(s)):
while (r_end<len(s)):
ch = s[r_end]
if ch in list(word_dic.keys()):
word_dic[ch] += 1
else:
if len(list(word_dic.keys())) < maxLetters:
word_dic[ch] = 1
else:
break
r_end += 1
for j in range(minSize,min(maxSize,r_end-i) + 1):
subs = s[i:i+j]
if subs not in str_dic:
str_dic[subs] = 1
else:
str_dic[subs] += 1
word_dic[s[i]] -= 1
if word_dic[s[i]] == 0:
del word_dic[s[i]]
if not bool(str_dic):
return 0
else:
return max(str_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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
seen = defaultdict(lambda : 0)
m = defaultdict(lambda : 0)
left = 0
for i in range(len(s)):
m[s[i]] += 1
while(len(m) > maxLetters or i-left+1 > maxSize):
m[s[left]] -= 1
if m[s[left]] == 0:
m.pop(s[left])
left += 1
temp = left
while i-temp+1 >= minSize:
seen[s[temp:i+1]] += 1
temp += 1
if not seen:
return 0
return max(list(seen.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:
T = lambda: collections.defaultdict( T )
trie , res = T( ) , 0
data = [ [ trie , set( ) ] for _ in range( len( s ) - minSize + 1 ) ]
for i in range( minSize ) :
for j in range( len( s ) - minSize + 1 ) :
c = s[i+j]
cur = data[j]
cur[1].add(c)
if i == minSize - 1 and len( cur[ 1 ] ) <= maxLetters :
cur[0][c].setdefault( '#' , 0 )
cur[0][c]['#'] += 1
res = max( res , cur[ 0 ][ c ][ '#' ] )
cur[ 0 ] = cur[0][c]
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.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
letdict = defaultdict(int)
subdict = defaultdict(int)
uniquecount = 0
start = end = 0
n = len(s)
while end < n:
val = s[end]
letdict[val] += 1
if letdict[val] == 1:
uniquecount += 1
while uniquecount > maxLetters or ((end-start)+1) > maxSize:
val = s[start]
letdict[val] -= 1
if letdict[val] == 0:
uniquecount -= 1
start += 1
tstart = start
while ((end-tstart)+1) >= minSize:
subdict[s[tstart:end+1]] += 1
tstart += 1
end += 1
if not subdict:
return 0
return subdict[max(subdict, key = lambda x: subdict[x])]
|
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
cnt=defaultdict(int)
for i in range(minSize-1,len(s)):
low1=i-minSize+1
low2=i-maxSize+1
seen1=set()
seen2=set()
for k in range(low1,i+1):
if s[k] not in seen1:
seen1.add(s[k])
if low2>=0 and low1!=low2:
for k in range(low2,i+1):
if s[k] not in seen2:
seen2.add(s[k])
if len(seen2)<=maxLetters:
cnt[s[low2:i+1]]+=1
if len(seen1)<=maxLetters:
cnt[s[low1:i+1]]+=1
if list(cnt.values()):
return max(cnt.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 len(s) < minSize:
return 0
occur = {}
l,r=0, minSize
while r <= len(s):
sub = s[l:r]
if occur.get(sub) is None:
distinct = set(sub)
if len(distinct) <= maxLetters:
occur[sub] = 1
else:
occur[sub]+=1
l += 1
r += 1
return max(occur.values()) if len(occur) >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:
from collections import defaultdict
res = 0
valid_str_freq = defaultdict(int)
window_letter = {}
left = 0
right = 0
for right in range(0, len(s), 1):
# print(left, right)
for i in range(left, right, 1):
if right - i < minSize:
break
valid_str = s[i:right]
# print(i, right, valid_str)
valid_str_freq[valid_str] += 1
res = max(res, valid_str_freq[valid_str])
# add right
if s[right] not in window_letter:
window_letter[s[right]] = 1
else:
window_letter[s[right]] += 1
# check left
while (len(window_letter) > maxLetters
or right - left + 1 > maxSize
):
window_letter[s[left]] -= 1
if window_letter[s[left]] == 0:
del window_letter[s[left]]
left += 1
right += 1
for i in range(left, right, 1):
if right - i < minSize:
break
valid_str = s[i:right]
# print(i, right, valid_str)
valid_str_freq[valid_str] += 1
res = max(res, valid_str_freq[valid_str])
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:
res = collections.Counter()
b=0
cc = collections.Counter()
for e in range(len(s)):
cc[s[e]] += 1
while len(cc)>maxLetters or e-b+1>maxSize:
cc[s[b]] -= 1
if cc[s[b]]==0: cc.pop(s[b])
b += 1
i=b
while e-i+1>=minSize:
res[s[i:e+1]] += 1
i += 1
# print(res)
return res.most_common(1)[0][1] 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: str, maxLetters: int, minSize: int, maxSize: int) -> int:
d = defaultdict(int)
for i in range(len(s)-minSize+1):
letters = set(s[i:i+minSize-1])
for j in range(minSize, min(maxSize, len(s)-i)+1):
letters.add(s[i+j-1])
if len(letters) > maxLetters:
break
d[s[i:i+j]] += 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = defaultdict(lambda: 0)
m = ''
n = len(s)
for i in range(n - minSize + 1):
end = i + minSize
unique = set(s[i:end])
while end <= n:
unique.add(s[end - 1])
if len(unique) > maxLetters:
break
cur = s[i:end]
freq[cur] += 1
if freq[cur] > freq[m]:
m = cur
end += 1
return freq[m]
|
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 = defaultdict(lambda: 0)
m = ''
uniques = defaultdict(lambda: set())
n = len(s)
for i in range(n - minSize + 1):
end = i + minSize
unique = set(s[i:end])
while end <= n:
unique.add(s[end - 1])
if len(unique) > maxLetters:
break
cur = s[i:end]
freq[cur] += 1
if freq[cur] > freq[m]:
m = cur
end += 1
return freq[m]
|
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 = Counter()
cnt = Counter()
j = 0
for i, ss in enumerate(s):
cnt[ss] += 1
while len(cnt) > maxLetters and j <= i:
cnt[s[j]] -= 1
if not cnt[s[j]]:
del cnt[s[j]]
j += 1
k = j
while i - k + 1 >= minSize:
if i - k + 1 <= maxSize:
seen[s[k: i + 1]] += 1
k += 1
return max(seen.values()) if seen 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:
ret = defaultdict(int)
for i in range(len(s)):
temp = ''
char = set()
for j in range(i, len(s)):
temp = temp + s[j]
char.add(s[j])
if len(char) <= maxLetters and minSize <= len(temp) <= maxSize:
ret[temp] += 1
elif len(char) > maxLetters or len(temp) > maxSize:
break
if len(ret) == 0:
return 0
return max(ret.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=collections.defaultdict(int)
for i in range(0,n-minSize+1):
temp=s[i:i+minSize]
c=set(temp)
if len(c)<=maxLetters:
d[temp]+=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 defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
mx_count = 0
def create_dct(size, mx_count):
dct = defaultdict(int)
i = 0
j = size - 1
while j < len(s):
temp = []
temp_dict = defaultdict(int)
flag = True
for x in range(i, j+1):
temp.append(s[x])
temp_dict[s[x]] += 1
if len(temp_dict) > maxLetters:
flag = False
break
i += 1
j += 1
if not flag:
continue
tmp_string = ''.join(temp)
dct[tmp_string] += 1
if dct[tmp_string] > mx_count:
mx_count = dct[tmp_string]
return mx_count
mx_count = create_dct(minSize, mx_count)
mx_count = max(create_dct(maxSize, mx_count), mx_count)
return mx_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:
c = Counter()
i = 0
letterlen = 0
maxSize = maxSize+1 if maxSize==minSize else maxSize
ans = Counter()
for j, v in enumerate(s):
c[v] += 1
if c[v] == 1:
letterlen += 1
while letterlen > maxLetters:
x = s[i]
c[x] -= 1
if c[x] == 0:
letterlen -= 1
i += 1
#print(i,j)
for l in range(minSize,maxSize):
beg = j-l +1
#print(i,j,beg)
if beg >= i:
ans[s[beg:j+1]] += 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
count = 0
n = len(s)
count = Counter()
for i in range(n - minSize + 1):
r = i + minSize
seen = {c for c in s[i:r]}
unique = len(seen)
while unique <= maxLetters and r <= n and r - i <= maxSize:
if s[r - 1] not in seen:
unique += 1
seen.add(s[r-1])
count[s[i:r]] += 1
r += 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if minSize > len(s): return 0
left= 0
candidates = Counter()
while left <= len(s) - minSize:
right = left + minSize
count = set(s[left:right])
while right <= len(s) and right-left <= maxSize and len(count) <= maxLetters:
if right < len(s):
count.add(s[right])
candidates[s[left:right]] += 1
right += 1
left += 1
if not candidates:
return 0
return max(candidates.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:
feq = collections.Counter()
N = len(s)
for i in range(N):
letters = set([c for c in s[i : i + minSize - 1]])
for j in range(minSize, maxSize + 1):
k = i + j
if k > N:
break
letters.add(s[k - 1])
if len(letters) > maxLetters:
break
feq[s[i:k]] += 1
return max(feq.values()) if list(feq.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, mxl: int, mns: int, mxs: int) -> int:
freq = collections.Counter()
for i in range(mns, len(s)+1):
for j in range(mxs-mns+1):
if i + j > len(s):
break
curr = s[i-mns:i+j]
if len(set(curr)) > mxl:
break
freq[curr] += 1
return max(freq.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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
from collections import defaultdict
letters = defaultdict(int)
res = 0
if len(s) < minSize:
return 0
i = 0
j = 0
unique = 0
counter = defaultdict(int)
while i < len(s):
if letters[s[i]] == 0:
unique += 1
letters[s[i]] += 1
while j < i and unique > maxLetters:
letters[s[j]] -= 1
if letters[s[j]] == 0:
unique -= 1
j += 1
j_tmp = j
unique_tmp = unique
letters_tmp = letters.copy()
while unique_tmp <= maxLetters and (minSize <= (i - j_tmp + 1)):
if (i - j_tmp + 1) > maxSize:
j_tmp+=1
continue
counter[s[j_tmp:i+1]] += 1
#print(f'{s[j_tmp:i+1]} {counter} {i} {j_tmp}')
letters_tmp[s[j_tmp]] -= 1
if letters_tmp[s[j_tmp]] == 0:
unique_tmp -= 1
j_tmp += 1
#print(f'i {i} j {j} unique {unique} {s[j:i+1]} letters {letters},res {res}, len {i-j+1}, {counter}')
i += 1
if len(counter.values()) == 0:
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:
start = 0
end = minSize
counts = {}
res = 0
while start <= len(s)-minSize:
item = s[start:end]
counts[item] = counts.get(item, 0) + 1
start += 1
end += 1
for i in counts:
if self.countUnique(i) <= maxLetters:
res = max(res, counts[i])
return res
def countUnique(self, s):
return len(set(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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
ln_s = len(s);
ans = 0;
seen = Counter();
for i in range( ln_s):
set_buff = set(s[i:i+minSize]);
for j in range(i+minSize-1, min(ln_s, i+maxSize)):
buff = s[i:j+1];
set_buff.add( s[j]);
if len(set_buff) > maxLetters: break;
seen[buff] += 1;
ans = max( ans, seen[buff]);
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:
subs = {}
i = j = 0
chars = {}
while i < len(s):
if j < len(s) and (s[j] in chars or len(chars) < maxLetters):
if s[j] not in chars:
chars[s[j]] = 0
chars[s[j]] += 1
j += 1
else:
for k in range(i+minSize, min(i+maxSize, j)+1):
sub = s[i:k]
if sub not in subs:
subs[sub] = 0
subs[sub] += 1
chars[s[i]] -= 1
if chars[s[i]] == 0:
del chars[s[i]]
i += 1
return max(list(subs.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.
|
class Solution:
def maxFreq(self, s, maxLetters, minSize, maxSize):
if minSize > len(s):
return 0
left = 0
right = minSize - 1
words = {}
while left < len(s) - minSize + 1:
word = s[left:right+1]
while right < len(s) and right - left < maxSize and len(set(word)) <= maxLetters:
if word not in words:
words[word] = 0
words[word] += 1
right += 1
if right < len(s):
word += s[right]
left += 1
right = left + minSize - 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:
d = dict()
n = len(s)
for i in range(n):
j = i
st = set()
l = 0
while j<min(n,i+maxSize) and l<=maxLetters:
if s[j] not in st:
l+=1
st.add(s[j])
x = s[i:j+1]
ln = j-i+1
#print(x,ln)
if ln>=minSize and ln<=maxSize and l<=maxLetters:
if x in d:
d[x]+=1
else:
d[x]=1
j+=1
if d==dict():
return 0
return max(list(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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
N = len(s)
d1 = {}
maxval = 0
for i in range(N-minSize+1):
d = {}
for j in range(i,i+minSize):
if(s[j] not in d):
d[s[j]] = 1
else:
d[s[j]] += 1
if(len(d)<=maxLetters):
if(s[i:i+minSize] not in d1):
d1[s[i:i+minSize]] = 1
else:
d1[s[i:i+minSize]] += 1
maxval = max(maxval,d1[s[i:i+minSize]])
else:
continue
for j in range(i+minSize, min(i+maxSize,N)):
if(s[j] not in d):
d[s[j]] = 1
else:
d[s[j]] += 1
if(len(d)<=maxLetters):
if(s[i:j+1] not in list(d1.keys())):
d1[s[i:j+1]] = 1
else:
d1[s[i:j+1]] += 1
maxval = max(maxval,d1[s[i:j+1]])
else:
break
return maxval
|
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:
ans , freq_subs , n = collections.defaultdict( int ) , 0 , len( s )
for i in range( n - minSize + 1 ) :
for j in range( i + minSize , n + 1 ) :
if len( set( s[ i : j ] ) ) <= maxLetters :
if s[i:j] in ans :
ans[ s[i:j] ] += 1
freq_subs = max( freq_subs , ans[s[i:j]] )
else :
ans[s[i:j]] = 1
freq_subs = max( freq_subs , 1 )
else :
break
return freq_subs
|
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 + maxSize):
if rightInd > len(s) - 1:
break
seen.add(s[rightInd])
if len(seen) > maxLetters:
break
if maxSize >= 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cache=collections.defaultdict(int)
for i in range(len(s)+1-minSize):
if len(set(s[i:i+minSize]))<=maxLetters:
cache[s[i:i+minSize]]+=1
res=0
for k,v in list(cache.items()):
res=max(res,v)
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:
d=defaultdict(int)
s=list(s)
n=len(s)
for i in range(n-minSize+1):
for j in range(i+minSize,min(i+maxSize+1,n+1)):
if len(set(s[i:j]))<=maxLetters:
d[tuple(s[i:j])]+=1
else:
break
if not d:
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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
if minSize > len(s):
return 0
maxCount = 0
seenSubstrs = Counter()
for i in range(len(s)):
letterSet = set()
for j in range(i, i + minSize - 1):
if j >= len(s):
break
letterSet.add(s[j])
if len(letterSet) > maxLetters:
break
if len(letterSet) > maxLetters:
continue
for j in range(i+minSize-1, i+maxSize+1):
if j >= len(s):
break
letterSet.add(s[j])
if len(letterSet) > maxLetters:
break
seenSubstrs[s[i:j+1]] += 1
maxCount = max(seenSubstrs[s[i:j+1]], maxCount)
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:
sub = dict()
for i in range(len(s) - minSize + 1):
d = dict()
for k in range(i, i + minSize):
if s[k] in d:
d[s[k]] += 1
else:
d[s[k]] = 1
if len(d) <= maxLetters:
phrase = s[i:i+minSize]
if phrase in sub:
sub[phrase] += 1
else:
sub[phrase] = 1
else:
continue
for j in range(i + minSize, i + maxSize):
if j < len(s):
if len(d) <= maxLetters:
phrase = s[i:j+1]
if phrase in sub:
sub[phrase] += 1
else:
sub[phrase] = 1
else:
break
if s[j] in d:
d[s[j]] += 1
else:
d[s[j]] = 1
if not sub:
return 0
return max(sub.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)
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 = set(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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
def valid(sub):
seen = set()
for c in sub: seen.add(c)
return len(seen)
counts = dict()
start = 0
while start < len(s):
end = start+minSize
while end <= len(s) and end <= start + maxSize:
sub = s[start:end]
if sub in counts:
counts[sub] += 1
else:
num_letters = valid(sub)
if num_letters <= maxLetters:
counts[sub] = 1
else:
break
end += 1
start += 1
l = list(counts.values())
if len(l) == 0: return 0
return max(l)
|
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:
all_substrings = {}
max_letters = maxLetters
min_size = minSize
max_size = maxSize
_s = s
ll = len(_s)
for i in range(min_size, max_size + 1):
for j in range(ll - i + 1):
ss = _s[j:j+i]
if max_letters >= min_size or len(set(ss)) <= max_letters:
if ss not in all_substrings:
all_substrings[ss] = 1
else:
all_substrings[ss] += 1
return max(all_substrings.values()) if len(all_substrings) > 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:
sol = {}
for i in range(len(s) - minSize + 1):
count = set()
length = 0
for j in range(minSize - 1):
length += 1
c = s[i+j]
if c not in count:
count.add(c)
while length < maxSize and (i + length) < len(s):
c = s[i + length]
if c not in count:
count.add(c)
if len(count) <= maxLetters:
substring = s[i: i+length+1]
if substring in sol:
sol[substring] += 1
else:
sol[substring] = 1
length += 1
maximum = 0
for substring in sol:
if sol[substring] > maximum:
maximum = sol[substring]
return 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cur_soln = {s[:minSize]: 1}
for end in range(minSize, len(s)):
for start in range(end-minSize+1, max(-1, end-maxSize), -1):
subs = s[start:end+1]
cur_soln[subs] = cur_soln.get(subs, 0) + 1
cur_best = 0
for s, cnt in list(cur_soln.items()):
if cnt > cur_best and len(set(s)) <= maxLetters:
cur_best = cnt
return cur_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:
count = collections.Counter()
for i in range(len(s) - minSize + 1):
t = s[i:minSize+i]
if len(set(t)) <= maxLetters:
count[t] += 1
if count:
return max(count.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:
maxcount = 0
visited = {}
for i in range(len(s)):
for j in range(minSize, minSize+1):
now = s[i:i+j]
if now in visited:
continue
visited[now] = 1
if i + j > len(s):
break
nowset = set(now)
if len(nowset) > maxLetters:
break
count = 1
start = i+1
while(start < len(s)):
pos = s.find(now, start)
if pos != -1:
start = pos + 1
count += 1
else:
break
maxcount = max(maxcount, count)
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:
substrings = defaultdict(int)
for gap in range(minSize, maxSize + 1):
for start in range(len(s) - gap + 1):
end = start + gap
substrings[s[start:end]] += 1
max_ = 0
for substring, times in substrings.items():
if times > max_ and len(set(substring)) <= maxLetters:
max_ = times
return max_
|
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 maxLetters == 0:
return 0
sw = collections.defaultdict(int)
substrings = collections.defaultdict(int)
l = 0
res = 0
for r, ch in enumerate(s):
sw[ch] += 1
while l <= r and len(sw) > maxLetters:
chL = s[l]
sw[chL] -= 1
if sw[chL] == 0:
del sw[chL]
l += 1
#print(r, r + 1 - maxSize, r + 1 - minSize + 1)
#print(l, r)
for j in range(r + 1 - maxSize, r + 1 - minSize + 1):
if j < l:
continue
substrings[s[j : r + 1]] += 1
res = max(res, substrings[s[j : r + 1]])
#print(substrings)
return res
#l r
|
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:
maxi = 0
for index in range(minSize, maxSize + 1):
result = self.getAllSubstringsWithRules(s, maxLetters, index)
if result > maxi:
maxi = result
return maxi
def getAllSubstringsWithRules(self, s, maxLetters, size):
length = len(s)
letters = {}
subStrings = {}
for index in range(size):
letter = s[index]
if letter not in letters:
letters[letter] = 0
letters[letter] += 1
if len(letters) <= maxLetters:
subStrings[s[:size]] = 1
for index in range(size, length):
letterToRemove = s[index - size]
letters[letterToRemove] -= 1
if letters[letterToRemove] == 0:
del letters[letterToRemove]
letterToAdd = s[index]
if letterToAdd not in letters:
letters[letterToAdd] = 0
letters[letterToAdd] += 1
if len(letters) <= maxLetters:
string = s[index - size + 1:index + 1]
if string not in subStrings:
subStrings[string] = 0
subStrings[string] += 1
if not subStrings:
return 0
return max(subStrings.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)
cnt = defaultdict(int)
for i in range(n):
now = ''
se = set()
for j in range(maxSize):
if i+j>=n:
break
now += s[i+j]
se.add(s[i+j])
if len(se)<=maxLetters and len(now)>=minSize:
cnt[now] += 1
ans = 0
for v in cnt.values():
ans = max(ans, v)
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:
sol = {}
for i in range(len(s) - minSize + 1):
count = {}
length = 0
for j in range(minSize - 1):
length += 1
c = s[i+j]
if c in count:
count[c] += 1
else:
count[c] = 1
while length < maxSize and (i + length) < len(s):
c = s[i + length]
if c in count:
count[c] += 1
else:
count[c] = 1
if len(count) <= maxLetters:
substring = s[i: i+length+1]
if substring in sol:
sol[substring] += 1
else:
sol[substring] = 1
length += 1
maximum = 0
for substring in sol:
if sol[substring] > maximum:
maximum = sol[substring]
return 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
cnt=collections.Counter()
for i in range (len(s)-minSize+1):
sub=s[i:i+minSize]
if len(set(sub))<=maxLetters:
cnt[sub]+=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:
sol = {}
for i in range(len(s) - minSize + 1):
count = set()
length = 0
while length < maxSize and (i + length) < len(s):
c = s[i + length]
if c not in count:
count.add(c)
length += 1
# print(s[i: i+length+1])
if len(count) <= maxLetters and length >= minSize:
substring = s[i: i+length]
if substring in sol:
sol[substring] += 1
else:
sol[substring] = 1
maximum = 0
for substring in sol:
if sol[substring] > maximum:
maximum = sol[substring]
return 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.
|
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-1):
c = s[i]
window[c] = window.get(c, 0) + 1
for i in range(current_size-1, len(s)):
start = i - current_size
# add current
c = s[i]
window[c] = window.get(c, 0) + 1
# remove tail
if start >= 0:
c = s[start]
window[c] -= 1
if window[c] == 0:
del window[c]
# check
if len(window) <= maxLetters:
sub = s[start+1:i+1]
counts[sub] = counts.get(sub, 0) + 1
# 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:
dic = {}
for outer in range(0,len(s)):
if minSize + outer > len(s): break
substring = s[outer:minSize+outer]
while len(substring) <= maxSize and len(set(substring))<=maxLetters:
if dic.get(substring):
dic[substring] += 1
else: dic[substring] = 1
newIndex = outer + len(substring) + 1
if not newIndex > len(s):
substring = s[outer:newIndex]
else: break
if dic:
maxKey = max(dic,key=lambda key: dic[key])
return dic[maxKey]
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:
for i in range(minSize, maxSize+1):
max_freq = self.maxFreqSetSize(s, maxLetters, i)
if max_freq > 0:
return max_freq
return 0
def maxFreqSetSize(self, s, maxLetters, windowSize):
valid_substrings = set()
maxFreq = 0
for i in range(len(s)-windowSize):
substring = s[i:i+windowSize]
if substring in valid_substrings:
continue
unique_letters = set()
for k in range(windowSize):
letter = s[i+k]
unique_letters.add(letter)
if len(unique_letters) > maxLetters:
continue
frequency = 1
pos = i + 1
while pos > -1:
new_pos = s[pos:].find(substring)
if new_pos == -1:
break
frequency += 1
pos += new_pos + 1
if frequency > maxFreq:
maxFreq = frequency
valid_substrings.add(substring)
return maxFreq
|
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:
mymap = collections.defaultdict(int)
st, j, n = 0, 0, len(s)
# charmap = collections.defaultdict(int)
# for i in range(st, st+minSize-1):
# charmap[s[i]] += 1
# # print(charmap)
while st + minSize <= n:
# print (st)
count = collections.Counter(s[st:st+minSize-1])
for j in range(st+minSize-1, st+maxSize):
if j >= n:
break
count[s[j]] += 1
if len(count) <= maxLetters:
mymap[s[st:j+1]] += 1
# print(j, s[st:])
# if j >= n:
# break
# charmap[s[j]] += 1
# if len(charmap) <= maxLetters:
# mymap[s[st:j+1]] += 1
# charmap[s[st]] -= 1
# if charmap[s[st]] <= 0:
# charmap.pop(s[st])
st += 1
# ans = 0
maxval = max(list(mymap.values()) or [0])
# for key in mymap:
# if mymap[key] == maxval:
# ans += 1
return maxval
# print(mymap)
|
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:
validWords = {}
for i in range(0, len(s)):
for j in range(i + minSize - 1, min(i + maxSize, len(s))):
ss = s[i:j + 1]
if len(set(ss)) <= maxLetters:
if ss in validWords:
validWords[ss] += 1
else:
validWords[ss] = 1
# print(\"valid: \", validWords)
if validWords:
all_values = validWords.values()
return max(all_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:
mymap = collections.defaultdict(int)
st, j, n = 0, 0, len(s)
while st + minSize <= n:
# print (st)
count = collections.Counter(s[st:st+minSize-1])
for j in range(st+minSize-1, st+maxSize):
if j >= n:
break
count[s[j]] += 1
if len(count) <= maxLetters:
mymap[s[st:j+1]] += 1
st += 1
maxval = max(list(mymap.values()) or [0])
return maxval
# mymap = collections.defaultdict(int)
# st, j, n = 0, 0, len(s)
# # charmap = collections.defaultdict(int)
# # for i in range(st, st+minSize-1):
# # charmap[s[i]] += 1
# # # print(charmap)
# while st + minSize <= n:
# # print (st)
# count = collections.Counter(s[st:st+minSize-1])
# for j in range(st+minSize-1, st+maxSize):
# if j >= n:
# break
# count[s[j]] += 1
# if len(count) <= maxLetters:
# mymap[s[st:j+1]] += 1
# # print(j, s[st:])
# # if j >= n:
# # break
# # charmap[s[j]] += 1
# # if len(charmap) <= maxLetters:
# # mymap[s[st:j+1]] += 1
# # charmap[s[st]] -= 1
# # if charmap[s[st]] <= 0:
# # charmap.pop(s[st])
# st += 1
# # ans = 0
# maxval = max(mymap.values() or [0])
# # for key in mymap:
# # if mymap[key] == maxval:
# # ans += 1
# return maxval
# # print(mymap)
|
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:
maxOcc = 0
strOcc = {}
for i in range(minSize, maxSize + 1):
charFreq = {}
sub = s[:i]
uniqueChar = 0
for c in sub:
if c not in charFreq:
charFreq[c] = 0
uniqueChar += 1
charFreq[c] += 1
if uniqueChar <= maxLetters:
if sub not in strOcc:
strOcc[sub] = 0
strOcc[sub] += 1
maxOcc = max(maxOcc, strOcc[sub])
for j in range(i, len(s)):
outC = sub[0]
inC = s[j]
charFreq[outC] -= 1
if charFreq[outC] == 0:
uniqueChar -= 1
del charFreq[outC]
if inC not in charFreq:
charFreq[inC] = 0
uniqueChar += 1
charFreq[inC] += 1
sub = sub[1:] + inC
if uniqueChar <= maxLetters:
if sub not in strOcc:
strOcc[sub] = 0
strOcc[sub] += 1
maxOcc = max(maxOcc, strOcc[sub])
return maxOcc
|
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)
if n < minSize:
return 0
c = collections.Counter()
for start in range(n - minSize+1):
temp = s[start:start+minSize]
tc = collections.Counter(temp)
if len(tc.keys()) <= maxLetters:
c[temp] += 1
else:
continue
for i in range(start+minSize,min(n,start + maxSize)):
tc[s[i]] += 1
temp += s[i]
if len(tc.keys()) <= maxLetters:
c[temp] += 1
else:
continue
return max(c.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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substr = defaultdict(int)
n = len(s)
unique = set()
max_freq = 0
for i in range(n - minSize + 1):
current_str = s[i:i+minSize]
if len(set(current_str)) <= maxLetters:
substr[current_str] += 1
max_freq = max(max_freq, substr[current_str])
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.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substrFreq = defaultdict(int)
charFreq = defaultdict(int)
curr_substr = []
l = 0
r = l + minSize - 1
while l < len(s):
for ch in s[l:r]:
curr_substr.append(ch)
charFreq[ch] += 1
while (r - l + 1) <= maxSize and r < len(s):
charFreq[s[r]] += 1
curr_substr.append(s[r])
if len(charFreq) <= maxLetters:
# print(curr_substr)
substrFreq[''.join(curr_substr)] += 1
r += 1
curr_substr = []
charFreq = defaultdict(int)
l += 1
r = l + minSize - 1
if substrFreq:
return max(substrFreq.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.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
finalDict = collections.defaultdict(int)
for i in range(len(s)):
for j in range(i + minSize - 1, min(i + maxSize, len(s))):
substring = s[i:j+1]
if len(set(substring)) <= maxLetters:
finalDict[substring] += 1
return max(finalDict.values()) if finalDict 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 size in range(minSize, maxSize+1):
for i in range(len(s) - size + 1):
sub_str = s[i:i+size]
if len(set(sub_str)) > maxLetters: continue
if sub_str in freq:
freq[sub_str] += 1
else:
freq[sub_str] = 1
max_freq = 0
for sub_str, cnt in list(freq.items()):
max_freq = max(max_freq, cnt)
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.
|
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(len(s)):
for j in range(i + minSize - 1, min(i + maxSize, len(s))):
sub = s[i:j + 1]
if len(set(sub)) <= maxLetters:
dic[sub] += 1
if len(dic):
return max(dic.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.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
substrFreq = defaultdict(int)
charFreq = defaultdict(int)
curr_substr = []
l = 0
r = l + minSize - 1
while l < len(s):
for ch in s[l:r]:
curr_substr.append(ch)
charFreq[ch] += 1
while (r - l + 1) <= maxSize and r < len(s):
charFreq[s[r]] += 1
curr_substr.append(s[r])
if len(charFreq) <= maxLetters:
# print(curr_substr)
substrFreq[''.join(curr_substr)] += 1
r += 1
curr_substr = []
charFreq = defaultdict(int)
l += 1
r = l + minSize - 1
mval = 0
mstr = None
for s, v in substrFreq.items():
if v > mval:
mstr = s
mval = v
return mval
|
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
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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
# brute force this damn thing
# 1. all substrings
freq = defaultdict(int)
best = 0
for l in range(minSize, maxSize+1):
for i in range(len(s)-l+1):
ss = s[i:i+l]
# print('substring', ss)
if len(set(ss)) <= maxLetters:
freq[ss] += 1
best = max(best, freq[ss])
# print(freq)
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 findSubstring(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> str:
len_s = len(s)
substrings = {}
for i in range(len_s):
for j in range(i+minSize, i+maxSize+1):
if j > len_s:
break
substring = s[i:j]
if len(set(substring)) <= maxLetters:
if substring not in substrings:
substrings[substring] = 0
substrings[substring] += 1
return substrings
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
len_s = len(s)
if len_s < minSize:
return 0
substrings = self.findSubstring(s, maxLetters, minSize, maxSize)
if not substrings:
return 0
return max(substrings.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 Counter
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
freq = Counter()
for k in range(minSize, maxSize + 1):
for i in range(len(s) - k + 1):
substring = s[i:i + k]
if len(set(substring)) <= maxLetters:
freq[substring] += 1
return max(freq.values()) if len(freq) > 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:
r = 0
seen = Counter()
for i in range(minSize, maxSize+1):
for j in range(i, len(s)+1):
t = s[j-i:j]
if len( set(t)) <= maxLetters:
# print(s[j-i:j])
seen[t] += 1
if seen[t] > r:
r = seen[t]
return r
|
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])
'''
If a string have occurrences x times,
any of its substring must appear at least x times.
There must be a substring of length minSize, that has the most occurrences.
So that we just need to count the occurrences of all substring with length minSize.'''
|
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_freq = 0
for l in range(minSize,maxSize+1):
hmap = {}
for i in range(len(s)-l+1):
if len(set(s[i:i+l])) <= maxLetters:
if s[i:i+l] in list(hmap.keys()):
hmap[s[i:i+l]] += 1
else:
hmap[s[i:i+l]] = 1
if list(hmap.keys()):
max_freq = max(max_freq,max(list(hmap.items()),key=lambda kv: kv[1])[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:
occ, n = collections.defaultdict(int), len(s)
for i in range(n):
for j in range(i + minSize - 1, min(i+maxSize, n)):
sub = s[i:j+1]
if len(set(sub)) <= maxLetters:
occ[sub] += 1
return max(occ.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, m: int, n: int, ss: int) -> int:
def getSubStrings(maxLetters:int, minSize: int, maxSize:int):
for i in range(len(s)):
for j in range(i+minSize, len(s) + 1):
if j - i > maxSize:
break
sub = s[i:j]
if len(set(sub)) <= maxLetters:
yield sub
counter = collections.defaultdict(int)
ret = 0
for substring in getSubStrings(m, n, ss):
counter[substring] += 1
ret = max(ret, counter[substring])
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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
toSearch={}
for size in range(minSize,maxSize+1):
for i in range(len(s)-size+1):
S=s[i:i+size]
letters=len(set(S))
if letters<=maxLetters:
if S in toSearch: toSearch[S]+=1
else: toSearch[S]=1
#print(toSearch)
ans=0
for e in toSearch:
ans=max(ans,toSearch[e])
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:
cnt=collections.Counter()
for size in range(minSize, maxSize+1):
for i in range(len(s)-size+1):
sub=s[i:i+size]
if len(set(sub))<=maxLetters:
cnt[sub]+=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:
occ, n = {} , len(s)
for i in range(n):
for j in range(i + minSize - 1, min(i + maxSize, n)):
sub = s[i:j + 1]
if len(set(sub)) <= maxLetters:
occ[sub] = occ.get(sub,0) +1
return(max(list(occ.values()) or [0]))
# ans = list(occ.values())
# ans.sort(reverse=True)
# return(ans[0] 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, maxLetters: int, minSize: int, maxSize: int) -> int:
counts = dict()
for i in range(minSize, maxSize+1):
for j in range(i, len(s)+1):
curr = s[j-i:j]
if curr not in counts:
if len(set(curr)) <= maxLetters:
counts[curr] = 1
else:
counts[curr] += 1
if not counts:
return 0
else:
return max(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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
possible = dict()
for winSize in range(minSize, maxSize + 1):
for winI in range(len(s) - winSize + 1):
win = s[winI: winI + winSize]
letters = set(win)
if len(letters) <= maxLetters:
if win in possible:
possible[win] += 1
else:
possible[win] = 1
# print(possible)
if possible:
return max(possible.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:
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:
# brute force this damn thing
# 1. all substrings
# if it works with greater than minSize, then must work with exactly minSize
freq = defaultdict(int)
best = 0
for i in range(len(s)-minSize+1):
ss = s[i:i+minSize]
# print('substring', ss)
if len(set(ss)) <= maxLetters:
freq[ss] += 1
best = max(best, freq[ss])
# print(freq)
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:
count = defaultdict(int)
for size in range(minSize, maxSize+1):
for i in range(0, len(s)-size+1):
st = s[i:size+i]
if len(set(st)) <= maxLetters:
count[st] += 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
SUB_LEN = len(s)
cache = defaultdict(int)
for start in range(SUB_LEN):
for end in range(start + minSize-1, min(start + maxSize, SUB_LEN)):
substring = s[start:end+1]
if len(set(substring)) <= maxLetters:
cache[substring] += 1
#print(cache.values())
return max(cache.values()) if cache 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:
# find subs that satisifes rules
found = collections.defaultdict(int)
for sl in range(minSize, min(len(s),maxSize)+1):
for start_index in range(len(s)-sl + 1):
substring = s[start_index:start_index+sl]
if len(set(substring)) <= maxLetters:
found[substring] += 1
vals = sorted(found.values())
if len(vals) == 0: return 0
return vals[-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)
results = 0
for l in range(minSize,maxSize+1):
if l<=n:
maps = {}
for i in range(n-l+1):
subs = s[i:i+l]
if len(set(subs))<=maxLetters:
try:
maps[subs] += 1
if maps[subs]>results:
results = maps[subs]
except KeyError:
maps[subs] = 1
if maps[subs]>results:
results = maps[subs]
return results
|
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:
substrings = []
for size in range(minSize, maxSize + 1):
for i in range(len(s)-size+1):
if len(set(s[i:i+size])) <= maxLetters:
substrings.append(s[i:i+size])
return collections.Counter(substrings).most_common(1)[0][1] if 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:
all_substrings = {}
for i in range(minSize, maxSize + 1):
for j in range(len(s) - i + 1):
ss = s[j:j+i]
if len(set(ss)) <= maxLetters:
if ss not in all_substrings:
all_substrings[ss] = 0
all_substrings[ss] += 1
if len(all_substrings) > 0:
return max(all_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.
|
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
if len(dic) > 0:
return max(dic.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.
|
import collections
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
counter = collections.defaultdict(int)
for ln in range(minSize,maxSize+1):
for i in range(0,len(s)-ln+1):
sub = s[i:i+ln]
if len(set(sub))<=maxLetters:
counter[sub] += 1
count = [item[1] for item in counter.items()]
return max(count) 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.
|
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
i = 0
lst = []
while i < len(s):
for j in range(minSize, maxSize+1):
subs = s[i:i+j]
# check the num of char
# c = collections.Counter(subs)
# for char in c:
# if c[char] > maxLetters:
# continue
sc = set(subs)
if len(sc) > maxLetters:
continue
if len(subs)>= minSize and len(subs)<=maxSize and i+j<=len(s):
lst.append(subs)
i+=1
# check the number of occurence of each subs
c = collections.Counter(lst)
maxs = 0
for subs2 in c:
maxs = max(c[subs2], maxs)
return maxs
|
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, a: int, b: int) -> int:
cnt = collections.defaultdict(int)
for i in range(len(s)-a+1):
cnt[s[i:i+a]]+=1
for a, v in sorted(cnt.items(), key = lambda x:x[1], reverse=True):
if len(set(a))<=maxLetters:return v
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:
res, occ = 0, collections.defaultdict(int)
for r in range(len(s) - minSize + 1):
sub = s[r:r+minSize]
if len(set(sub)) <= maxLetters:
occ[sub] += 1
res = max(res, occ[sub])
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.
|
from collections import defaultdict
class Solution:
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
d = defaultdict(int)
n = len(s)
for i in range(n):
# (i + minSize - 1, min(i + maxSize, n))
for j in range(i+minSize-1, min(i+maxSize, n)):
# print(s[i:j])
if len(set(s[i:j+1])) <= maxLetters:
d[s[i:j+1]]+=1
# print(d)
if d:
return max(d.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:
# minSize <= size <= maxSize
# uniqu <= maxLetters
# loop from 3 to 4
# aab abcaab
# aaba
# to find number of uniques, find the len of the set
# TIME: O(SN^2)
# SPACE: O(N)
# abcde length = 5
# size = 3
# abcde
ans = 0
counter = collections.Counter()
for size in range(minSize, maxSize+1):
for j in range(len(s)-size+1):
substring = s[j:j+size]
if len(set(substring)) <= maxLetters:
counter[substring]+=1
ans = max(ans,counter[substring])
# count = 1
# for k in range(j+1, len(s)-size+1):
# if substring == s[k:k+size]: count += 1
# ans = max(ans,count)
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:
all_substrings = {}
max_letters = maxLetters
min_size = minSize
max_size = maxSize
_s = s
max = 0
for i in range(min_size, max_size + 1):
for j in range(len(_s) - i + 1):
ss = _s[j:j+i]
if len(set(ss)) <= max_letters:
if ss not in all_substrings:
all_substrings[ss] = 0
all_substrings[ss] += 1
if all_substrings[ss] > max:
max = all_substrings[ss]
return max
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.