description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
d = {}
for i in S[:K]:
d[i] = d.get(i, 0) + 1
count = 0
window = K
slides = len(S) - window
if len(d) == K - 1:
count += 1
for j in range(slides):
Add = S[window]
Sub = S[j]
d[Add] = d.get(Add, 0) + 1
d[Sub] -= 1
if d.get(Sub) == 0:
d.pop(Sub)
if len(d) == K - 1:
count += 1
window += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
start = 0
end = 0
map = {}
count = 0
ans = 0
while end < len(S):
if S[end] not in map or not map[S[end]]:
count += 1
map[S[end]] = 1
else:
map[S[end]] += 1
if end - start + 1 < K:
end += 1
elif end - start + 1 == K:
if count == K - 1:
ans += 1
map[S[start]] -= 1
if not map[S[start]]:
count -= 1
start += 1
end += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
i = 0
j = 0
dic = {}
cnt = 0
rs = 0
while j <= len(S):
if j - i == K:
if len(dic) == K - 1:
rs += 1
if S[i] in dic:
if dic[S[i]] == 1:
del dic[S[i]]
cnt -= 1
elif dic[S[i]] == 2:
dic[S[i]] -= 1
cnt += 1
else:
dic[S[i]] -= 1
i += 1
elif j - i < K and j < len(S):
if S[j] in dic:
dic[S[j]] += 1
if dic[S[j]] == 2:
cnt -= 1
else:
dic[S[j]] = 1
cnt += 1
j += 1
else:
break
return rs
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
occurences = {}
l = 0
r = 0
distinct = K - 1
count = 0
n = len(S)
while r < n:
occurences[S[r]] = occurences.get(S[r], 0) + 1
if occurences[S[r]] == 1:
distinct -= 1
if r - l + 1 == K:
if distinct == 0:
count += 1
occurences[S[l]] -= 1
if occurences[S[l]] == 0:
distinct += 1
l += 1
r += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, k):
count = 0
d = {}
for x in S[:k]:
if d.get(x) == None:
d[x] = 0
d[x] += 1
count += len(d) == k - 1
for x in range(1, len(S) - k + 1):
rem = S[x - 1]
add = S[x + k - 1]
d[rem] -= 1
if d[rem] == 0:
del d[rem]
if d.get(add) == None:
d[add] = 0
d[add] += 1
count += len(d) == k - 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
ma = {}
coun = 0
for k in range(K):
if S[k] in ma:
ma[S[k]] += 1
else:
ma[S[k]] = 1
ans = 0
if len(ma) == K - 1:
ans = ans + 1
i = K
j = 0
while i <= len(S) - 1:
i = i + 1
j = j + 1
t_r = S[j - 1]
t_a = S[i - 1]
ma[t_r] -= 1
if ma[t_r] == 0:
del ma[t_r]
if t_a not in ma:
ma[t_a] = 1
else:
ma[t_a] += 1
if len(ma) == K - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, str, k):
N = len(str)
answer = 0
map = {}
for i in range(K):
try:
map[str[i]] += 1
except:
map[str[i]] = 1
if len(map) == K - 1:
answer += 1
for i in range(K, N):
try:
map[str[i]] += 1
except:
map[str[i]] = 1
map[str[i - K]] -= 1
if map[str[i - K]] == 0:
del map[str[i - K]]
if len(map) == K - 1:
answer += 1
return answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
ans = 0
hash = {}
for i in range(K):
hash[S[i]] = hash.get(S[i], 0) + 1
if len(hash) == K - 1:
ans += 1
for i in range(K, len(S)):
hash[S[i]] = hash.get(S[i], 0) + 1
hash[S[i - K]] -= 1
if hash[S[i - K]] == 0:
del hash[S[i - K]]
if len(hash) == K - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
count = {}
ctr = 0
ans = 0
for i in range(k):
if not count.get(s[i], 0):
ctr += 1
count[s[i]] = 1 + count.get(s[i], 0)
if ctr == k - 1:
ans += 1
for i in range(k, len(s)):
if count[s[i - k]] == 1:
ctr -= 1
count[s[i - k]] -= 1
if not count.get(s[i], 0):
ctr += 1
count[s[i]] = 1 + count.get(s[i], 0)
if ctr == k - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
n = len(S)
dp = [(0) for i in range(26)]
solve = 0
con = 0
for i in range(0, n):
c = ord(S[i]) - ord("a")
if dp[c] == 0:
con += 1
dp[c] += 1
if i >= K:
dp[ord(S[i - K]) - ord("a")] -= 1
if dp[ord(S[i - K]) - ord("a")] == 0:
con -= 1
if i >= K - 1 and con == K - 1:
solve += 1
return solve
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
d = dict()
for i in range(K):
d[S[i]] = d.get(S[i], 0) + 1
r = len(d)
c = 0
if r == K - 1:
c += 1
j = 0
for i in range(K, len(S)):
d[S[i]] = d.get(S[i], 0) + 1
d[S[j]] = d.get(S[j], 0) - 1
if d.get(S[j]) == 0:
d.pop(S[j])
if len(d) == K - 1:
c += 1
j += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
cnt = 0
d = {}
for i in range(K):
if S[i] not in d:
d[S[i]] = 1
else:
d[S[i]] += 1
if len(d) == K - 1:
cnt += 1
for j in range(K, len(S)):
if d[S[j - K]] == 1:
del d[S[j - K]]
else:
d[S[j - K]] -= 1
if S[j] not in d:
d[S[j]] = 1
else:
d[S[j]] += 1
if len(d) == K - 1:
cnt += 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
cons = 0
current_chars = {}
for i in range(K):
if current_chars.get(S[i], 0) == 0:
current_chars[S[i]] = 1
else:
current_chars[S[i]] += 1
if len(current_chars) == K - 1:
cons += 1
for i in range(K, len(S)):
first_char = S[i - K]
if current_chars[first_char] <= 1:
del current_chars[first_char]
else:
current_chars[first_char] -= 1
current_chars[S[i]] = current_chars.get(S[i], 0) + 1
if len(current_chars) == K - 1:
cons += 1
return cons
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
count = 0
dic = dict()
i, j = 0, 0
while j < len(S):
dic[S[j]] = dic.get(S[j], 0) + 1
if j - i + 1 == K:
if len(dic) == K - 1:
count += 1
elif j - i + 1 > K:
dic[S[i]] -= 1
if dic[S[i]] == 0:
del dic[S[i]]
i += 1
if len(dic) == K - 1:
count += 1
j += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
c = 0
d = dict()
for i in range(0, k):
if s[i] not in d:
d[s[i]] = 1
else:
d[s[i]] += 1
if len(d) == k - 1:
c += 1
j = 0
for i in range(k, len(s)):
d[s[j]] -= 1
if d[s[j]] == 0:
d.pop(s[j])
if s[i] not in d:
d[s[i]] = 1
else:
d[s[i]] += 1
if len(d) == k - 1:
c += 1
j += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
map1 = {}
count = 0
n = len(S)
if K == 1 and len(S) < 2:
return 0
for i in range(K):
if S[i] in map1:
map1[S[i]] += 1
else:
map1[S[i]] = 1
if len(map1) == K - 1:
count += 1
for i in range(K, n):
if S[i] in map1:
map1[S[i]] += 1
else:
map1[S[i]] = 1
map1[S[i - K]] -= 1
if map1[S[i - K]] == 0:
map1.pop(S[i - K])
if len(map1) == K - 1:
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
if K > 27:
return 0
count = 0
subs = ""
for i in range(len(S) - K + 1):
if i:
subs = subs + S[i + K - 1]
subs = subs[1:]
else:
subs = S[0:K]
if len(set(subs)) + 1 == K:
count += 1
return count
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, K):
n = len(s)
d = {}
c = 0
r = 0
for i in range(K):
if s[i] not in d:
d[s[i]] = 1
c += 1
else:
d[s[i]] += 1
if c == K - 1:
r += 1
for i in range(K, n):
d[s[i - K]] -= 1
if d[s[i - K]] == 0:
c -= 1
d.pop(s[i - K])
if s[i] not in d:
d[s[i]] = 1
c += 1
else:
d[s[i]] += 1
if c == K - 1:
r += 1
return r
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, k):
n = len(S)
count = 0
freq = {}
start = end = 0
while end < n:
ch = S[end]
freq[ch] = freq.get(ch, 0) + 1
if end - start + 1 > k:
ch = S[start]
freq[ch] -= 1
start += 1
if freq[ch] == 0:
freq.pop(ch)
if end - start + 1 == k and len(freq) == k - 1:
count += 1
end += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
ans = 0
cnt = 0
di = dict()
for i in range(K):
di[S[i]] = di.get(S[i], 0) + 1
if di[S[i]] == 1:
cnt += 1
if cnt == K - 1:
ans += 1
for i in range(K, len(S)):
di[S[i - K]] -= 1
if di[S[i - K]] == 0:
cnt -= 1
di[S[i]] = di.get(S[i], 0) + 1
if di[S[i]] == 1:
cnt += 1
if cnt == K - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def fun(self, K, S):
n = len(S)
count = 0
for i in range(0, n - K + 1):
j = i
s = ""
while j < K + i:
s = s + S[j]
j += 1
if len(set(s)) == K - 1:
count += 1
return count
def fun2(self, S, K):
ma = {}
coun = 0
for k in range(K):
if S[k] in ma:
ma[S[k]] += 1
else:
ma[S[k]] = 1
ans = 0
if len(ma) == K - 1:
ans = ans + 1
i = K
j = 0
while i <= len(S) - 1:
i = i + 1
j = j + 1
t_r = S[j - 1]
t_a = S[i - 1]
ma[t_r] -= 1
if ma[t_r] == 0:
del ma[t_r]
if t_a not in ma:
ma[t_a] = 1
else:
ma[t_a] += 1
if len(ma) == K - 1:
ans += 1
return ans
def countOfSubstrings(self, S, K):
return self.fun2(S, K)
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
i = 0
j = 0
dic = {}
count = 0
while j < len(s):
if s[j] in dic.keys():
t = dic.get(s[j])
dic[s[j]] = t + 1
else:
dic[s[j]] = 1
if j - i + 1 < k:
j = j + 1
elif j - i + 1 == k:
if len(dic) == k - 1:
count = count + 1
if dic.get(s[i]) == 1:
del dic[s[i]]
else:
u = dic.get(s[i])
dic[s[i]] = u - 1
i = i + 1
j = j + 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
d = dict()
count = 0
i = 0
j = 0
while j < len(S):
if S[j] not in d:
d[S[j]] = 1
else:
d[S[j]] += 1
if j - i + 1 == K:
if len(d) == K - 1:
count += 1
elif j - i + 1 > K:
d[S[i]] -= 1
if d[S[i]] == 0:
del d[S[i]]
i += 1
if j - i + 1 == K:
if len(d) == K - 1:
count += 1
j += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
d = {}
ans = 0
n = len(s)
j = 0
for i in range(k):
d[s[i]] = i
if len(d) == k - 1:
ans += 1
for i in range(k, n):
if d[s[j]] == j:
del d[s[j]]
j += 1
d[s[i]] = i
if len(d) == k - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
if K == 1:
return 0
dic = {}
n = len(S)
ans = 0
for i in range(K):
dic.setdefault(S[i], 0)
dic[S[i]] += 1
cnt = len(dic.keys())
if cnt == K - 1:
ans += 1
for i in range(K, n):
dic[S[i - K]] -= 1
if dic[S[i - K]] == 0:
cnt -= 1
if S[i] in dic.keys() and dic[S[i]] != 0:
dic[S[i]] += 1
else:
dic[S[i]] = 1
cnt += 1
if cnt == K - 1:
ans += 1
return ans
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
result = 0
uniq_chars = {}
for i in range(K):
if S[i] not in uniq_chars:
uniq_chars[S[i]] = 0
uniq_chars[S[i]] += 1
uniq_count = 0
for k, v in uniq_chars.items():
if v > 0:
uniq_count += 1
if uniq_count == K - 1:
result += 1
ibeg = 0
iend = K
while iend < len(S):
uniq_chars[S[ibeg]] -= 1
if S[iend] not in uniq_chars:
uniq_chars[S[iend]] = 0
uniq_chars[S[iend]] += 1
uniq_count = 0
for k, v in uniq_chars.items():
if v > 0:
uniq_count += 1
if uniq_count == K - 1:
result += 1
ibeg += 1
iend += 1
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
if K == 1:
return 0
small = {}
big = {}
i = 0
j = 0
idx = 0
res = 0
while idx < len(S):
while i < len(S) and not (len(small) == K - 2 and S[i] not in small):
if S[i] not in small:
small[S[i]] = 1
else:
small[S[i]] += 1
i += 1
while j < len(S) and not (len(big) == K - 1 and S[j] not in big):
if S[j] not in big:
big[S[j]] = 1
else:
big[S[j]] += 1
j += 1
if K - 2 == 0:
i = idx
if i - idx < K and j - 1 - idx >= K - 1:
res += 1
if S[idx] in small:
small[S[idx]] -= 1
if small[S[idx]] == 0:
small.pop(S[idx])
if S[idx] in big:
big[S[idx]] -= 1
if big[S[idx]] == 0:
big.pop(S[idx])
idx += 1
return res
|
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, k):
s = {}
count = 0
for i in range(len(S)):
if S[i] in s.keys():
s[S[i]] += 1
else:
s[S[i]] = 1
if i == k - 1:
if len(s) == k - 1:
count += 1
if i >= k:
s[S[i - k]] -= 1
if s[S[i - k]] == 0:
s.pop(S[i - k])
if len(s) == k - 1:
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
if k - 1 == 0:
return 0
dic = {}
i = 0
ct = 0
for j in range(len(s)):
dic[s[j]] = 1 + dic.get(s[j], 0)
if len(dic) == k - 1 and j - i + 1 == k:
ct += 1
dic[s[i]] -= 1
if dic[s[i]] == 0:
dic.pop(s[i])
i += 1
while j - i + 1 == k:
dic[s[i]] -= 1
if dic[s[i]] == 0:
dic.pop(s[i])
i += 1
return ct
|
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
ans = 0
left = 0
right = 0
freq = {}
while right < len(S):
freq[S[right]] = freq.get(S[right], 0) + 1
while right - left + 1 > k:
freq[S[left]] -= 1
if freq[S[left]] == 0:
del freq[S[left]]
left += 1
if right - left + 1 == k:
if len(freq) == k - 1:
ans += 1
right += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
k = K - 1
s = S
dict1 = {}
count = 0
j = 0
for i in range(len(s)):
if s[i] in dict1:
dict1[s[i]] += 1
else:
dict1[s[i]] = 1
while len(dict1) > k or sum(dict1.values()) > K:
dict1[s[j]] -= 1
if dict1[s[j]] == 0:
del dict1[s[j]]
j += 1
if len(dict1) == k and sum(dict1.values()) == K:
count += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, s, k):
i = j = 0
count = 0
mapper = dict()
n = len(s)
while j < n:
mapper[s[j]] = 1 + mapper.get(s[j], 0)
if j - i + 1 == k:
if len(mapper) == k - 1:
count += 1
if mapper[s[i]] - 1 == 0:
mapper.pop(s[i])
else:
mapper[s[i]] -= 1
i += 1
j += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, K):
d = {}
i, j = 0, 0
n = len(S)
ans = 0
while j < n:
if S[j] not in d:
d[S[j]] = 1
else:
d[S[j]] += 1
if j - i + 1 < K:
j += 1
elif j - i + 1 == K:
if len(d) == K - 1:
ans += 1
d[S[i]] -= 1
if d[S[i]] == 0:
del d[S[i]]
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a String S consisting only lowercase alphabets and an integer K. Find the count of all substrings of length K which have exactly K-1 distinct characters.
Example 1:
Input:
S = "abcc"
K = 2
Output:
1
Explanation:
Possible substrings of length K = 2 are
ab : 2 distinct characters
bc : 2 distinct characters
cc : 1 distinct character
Only one valid substring exists {"cc"}.
Example 2:
Input:
S = "aabab"
K = 3
Output :
3
Explanation:
Possible substrings of length K = 3 are
aab : 2 distinct characters
aba : 2 distinct characters
bab : 2 distinct characters.
All of these Substrings are a possible answer,
so the count is 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countOfSubstrings() which takes a String S and an integer K as input and returns the count of substrings of length K having K-1 distinct characters.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(constant)
Constraints:
1 ≤ K ≤ |S| ≤ 10^{5}
|
class Solution:
def countOfSubstrings(self, S, k):
n = len(S)
cnt = 0
hm = {}
for i in range(0, k):
hm[S[i]] = 1 + hm.get(S[i], 0)
if len(hm) == k - 1:
cnt += 1
i = 0
for j in range(k, n):
prev = S[i]
hm[prev] -= 1
if hm[prev] == 0:
del hm[prev]
i += 1
new = S[j]
hm[new] = 1 + hm.get(new, 0)
if len(hm) == k - 1:
cnt += 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
s = S
k = K
d = {}
i = 0
res = 0
maxe = 0
for j in range(0, len(s)):
d[s[j]] = d.get(s[j], 0) + 1
maxe = max(d[s[j]], maxe)
while j - i + 1 - maxe > k:
d[s[i]] = d[s[i]] - 1
i = i + 1
res = max(j - i + 1, res)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, arr, K):
start = end = answer = 0
max_count = 0
char_count = [0] * 26
while end < len(arr):
temp = ord(arr[end]) - ord("A")
char_count[temp] += 1
max_count = max(max_count, char_count[temp])
while end - start - max_count + 1 > K:
char_count[ord(arr[start]) - ord("A")] += -1
start += 1
answer = max(answer, end - start + 1)
end += 1
return answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
d = {}
res = 0
i = 0
n = len(S)
for j in range(n):
d[S[j]] = 1 + d.get(S[j], 0)
while j - i + 1 - max(d.values()) > K:
d[S[i]] -= 1
i += 1
res = max(j - i + 1, res)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, s, K):
n = len(s)
char_count = [(0) for _ in range(26)]
start = 0
res = 0
max_count = 0
for end in range(n):
char_count[ord(s[end]) - ord("A")] += 1
current_char_count = char_count[ord(s[end]) - ord("A")]
max_count = max(max_count, current_char_count)
while end - start - max_count + 1 > K:
char_count[ord(s[start]) - ord("A")] -= 1
start += 1
res = max(res, end - start + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
i, j = 0, 0
ans = 0
k = {}
while i < len(S):
if S[i] not in k:
k[S[i]] = 1
else:
k[S[i]] += 1
if i - j + 1 - max(k.values()) > K:
k[S[j]] -= 1
j += 1
ans = max(ans, i - j + 1)
i += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
l = 0
r = 0
maxFreq = 0
n = len(S)
ans = 0
dic = dict()
for r in range(n):
if S[r] not in dic:
dic[S[r]] = 0
dic[S[r]] += 1
maxFreq = max(maxFreq, dic[S[r]])
while r - l + 1 - maxFreq > K:
dic[S[l]] -= 1
l += 1
ans = max(ans, r - l + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
i = 0
j = 0
maxi = 0
d = {}
while j < len(S):
if S[j] in d:
d[S[j]] += 1
else:
d[S[j]] = 1
maxfreq = j - i + 1 - max(d.values())
if maxfreq <= K:
maxi = max(maxi, j - i + 1)
if maxfreq > K:
d[S[i]] -= 1
i = i + 1
j = j + 1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
d = {}
maxf, answer, i = 0, -1, 0
for j in range(len(S)):
if S[j] in d:
d[S[j]] += 1
else:
d[S[j]] = 1
maxf = max(maxf, d[S[j]])
while i < len(S) and j - i + 1 - maxf > K:
d[S[i]] -= 1
i += 1
answer = max(answer, j - i + 1)
return answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
count = [(0) for i in range(26)]
maxchar = 0
j = 0
i = 0
ans = 0
while j < len(S):
currchar = S[j]
count[ord(currchar) - 65] += 1
wsize = j - i + 1
maxchar = max(maxchar, count[ord(currchar) - 65])
rep = wsize - maxchar
if rep > K:
count[ord(S[i]) - 65] -= 1
i = i + 1
else:
ans = max(ans, j - i + 1)
j = j + 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
import sys
class Solution:
def characterReplacement(self, s, k):
windowStart = 0
maxLength = 0
maxCount = 0
d = {}
for windowEnd in range(len(s)):
rightChar = s[windowEnd]
if rightChar not in d:
d[rightChar] = 0
d[rightChar] += 1
maxCount = max(maxCount, d[rightChar])
if windowEnd + 1 - windowStart - maxCount > k:
leftChar = s[windowStart]
d[leftChar] -= 1
windowStart += 1
maxLength = max(maxLength, windowEnd + 1 - windowStart)
return maxLength
|
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
d = {}
ans = 1
r, l = 0, 0
while r < len(S):
i = S[r]
if i in d:
d[i] += 1
else:
d[i] = 1
while max(d.values()) + K < r - l + 1:
d[S[l]] -= 1
l += 1
ans = max(ans, r - l + 1)
r += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
l, h = 0, 0
d = {}
maxsize = 0
while h < len(S):
if S[h] not in d:
d[S[h]] = 1
else:
d[S[h]] += 1
maxf = max(list(d.values()))
capacity = h - l + 1 - maxf
while l <= h and capacity > K:
d[S[l]] -= 1
maxf = max(list(d.values()))
l += 1
capacity = h - l + 1 - maxf
maxsize = max(maxsize, h - l + 1)
h += 1
return maxsize
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
freq = [0] * 26
start = end = max_freq = result = 0
for end in range(len(S)):
freq[ord(S[end]) - ord("A")] += 1
max_freq = max(max_freq, freq[ord(S[end]) - ord("A")])
if end - start + 1 - max_freq > K:
freq[ord(S[start]) - ord("A")] -= 1
start += 1
result = max(result, end - start + 1)
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, s, k):
start = 0
fre_map = {}
max_fre = 0
ans = 0
for end in range(len(s)):
fre_map[s[end]] = fre_map.get(s[end], 0) + 1
max_fre = max(max_fre, fre_map[s[end]])
is_valid = end + 1 - start - max_fre <= k
if not is_valid:
fre_map[s[start]] -= 1
start += 1
ans = end + 1 - start
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
a = []
for i in range(26):
a.append(0)
maxi = 0
left = 0
right = 0
curlen = 0
while right < len(S):
curlen += 1
a[ord(S[right]) - 65] += 1
if curlen - max(a) <= K:
if curlen > maxi:
maxi = curlen
right += 1
else:
while curlen - max(a) > K:
curlen -= 1
a[ord(S[left]) - 65] -= 1
left += 1
right += 1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, s, k):
mp = [(0) for i in range(26)]
maxi, fp, lp = 0, 0, 0
ans = 0
while lp < len(s) and fp <= lp:
ind = ord(s[lp]) - 65
mp[ind] += 1
maxi = max(maxi, mp[ind])
cost = lp - fp + 1 - maxi
if cost <= k:
ans = max(ans, lp - fp + 1)
else:
while fp <= lp:
mp[ord(s[fp]) - 65] -= 1
fp += 1
maxi = max(maxi, mp[ind])
if lp - fp + 1 - maxi <= k:
ans = max(ans, lp - fp + 1)
break
lp += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, s, K):
n = len(s)
l, r = 0, 0
length, max_l = 1, 1
hm = {}
hm[s[0]] = 1
for i in range(1, n):
if s[i] not in hm:
hm[s[i]] = 1
else:
hm[s[i]] += 1
r += 1
maxx = 0
for _ in hm:
if hm[_] >= maxx:
maxx = hm[_]
length = r - l + 1
rem = length - maxx
if rem <= K:
pass
else:
hm[s[l]] += -1
l += 1
length = r - l + 1
max_l = max(max_l, length)
return max_l
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
low = 0
high = 0
ans = 0
count = 0
map = {}
while high < len(S):
if S[high] in map:
map[S[high]] += 1
else:
map[S[high]] = 1
count = max(count, map[S[high]])
if high - low + 1 - count > K:
map[S[low]] -= 1
low += 1
ans = max(ans, high - low + 1)
high += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, S, K):
window_start, max_len, max_repeate_letter_count, hash_map = 0, 0, 0, {}
for window_end in range(len(S)):
hash_map[S[window_end]] = 1 + hash_map.get(S[window_end], 0)
max_repeate_letter_count = max(
max_repeate_letter_count, hash_map[S[window_end]]
)
if window_end - window_start + 1 - max_repeate_letter_count > K:
hash_map[S[window_start]] -= 1
window_start += 1
max_len = max(max_len, window_end - window_start + 1)
return max_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string S and an integer K. In one operation, you are allowed to choose any character of the string and change it to any other uppercase English character.You can perform this operation atmost K times.
Return the length of the longest substring containing same letter you can get after performing the above operations.
Note : S consists of only uppercase English letters.
Example 1:
Input :
S = "ABBA"
K = 2
Output: 4
Explanation: Replace the 2 occurrences of 'A' with 2 'B's or vice-versa.
Example 2:
Input :
S = "AABAABB"
k = 2
Output: 6
Explanation: Replace the occurrence of 'B' with 'A' and form "AAAAAAB".
The substring "AAAAAA" has the most extended repeating letters, which is 6.
Your task :
You don't have to read input or print anything. Your task is to complete the function characterReplacement() which takes the string S and integer K as input and returns the longest substring containing same letter after performing specified operations.
Expected Time Complexity : (|S|)
Expected Auxiliary Space : O(1)
Constraints :
1 <= |S| <= 10^{5}
|
class Solution:
def characterReplacement(self, s, k):
n = len(s)
length = max_l = l = 0
dic = {s[0]: 1}
for r in range(1, n):
if s[r] in dic:
dic[s[r]] += 1
else:
dic[s[r]] = 1
maxx = 0
for key in dic:
if dic[key] >= maxx:
maxx = dic[key]
while r - l + 1 - maxx > k:
dic[s[l]] += -1
l += 1
maxx = 0
for key in dic:
if dic[key] >= maxx:
maxx = dic[key]
if r - l + 1 - maxx <= k:
max_l = max(r - l + 1, max_l)
return max_l
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
dict = {}
ans = 1000000000.0
for i in str:
if i not in dict:
dict[i] = 0
length = len(dict)
count = 0
j = 0
for i in range(len(str)):
dict[str[i]] += 1
if dict[str[i]] == 1:
count += 1
while count == length:
ans = min(ans, i - j + 1)
dict[str[j]] -= 1
if dict[str[j]] == 0:
count -= 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
m = {}
n = len(set(str))
length = float("inf")
j = 0
for i in range(len(str)):
m[str[i]] = m.get(str[i], 0) + 1
if len(m) == n:
while m[str[j]] > 1:
m[str[j]] -= 1
j += 1
length = min(length, i - j + 1)
return length
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
n = len(str)
ans = 0
length = n
s = list(set(str))
d = dict()
count = 0
start = 0
for i in range(n):
if str[i] not in d.keys():
d[str[i]] = 1
count += 1
else:
d[str[i]] += 1
if count == len(s):
while d[str[start]] > 1:
d[str[start]] -= 1
start += 1
ans = i - start + 1
if length > ans:
length = ans
return length
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
maxi = len(str)
sets = set(str)
i = 0
j = 0
m = {}
while i < len(str):
m[str[i]] = 1 + m.get(str[i], 0)
if len(m) >= len(sets):
while m[str[j]] > 1:
m[str[j]] -= 1
j += 1
maxi = min(maxi, i - j + 1)
i += 1
return maxi
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
res = 100000
d = {}
for i in range(len(str)):
if str[i] not in d:
d[str[i]] = 0
s1 = set()
count = len(d)
l = 0
for i in range(len(str)):
s1.add(str[i])
d[str[i]] = d[str[i]] + 1
while count == len(s1) and d[str[l]] != 0:
d[str[l]] = d[str[l]] - 1
if d[str[l]] == 0:
s1.remove(str[l])
res = min(res, i - l + 1)
l = l + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
unique = set(str)
res = len(str)
j = 0
map = dict()
for i in range(0, len(str)):
if str[i] in map.keys():
map[str[i]] += 1
else:
map[str[i]] = 1
if len(unique) == len(map):
while map[str[j]] > 1:
map[str[j]] -= 1
j += 1
res = min(res, i - j + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
k = len(set(str))
memo = {}
ans = len(str)
i, j = 0, 0
while j < len(str):
memo[str[j]] = memo.get(str[j], 0) + 1
if len(memo) < k:
j += 1
elif len(memo) == k:
while len(memo) == k:
memo[str[i]] -= 1
if memo[str[i]] == 0:
del memo[str[i]]
i += 1
ans = min(ans, j - i + 2)
j += 1
elif len(memo) > k:
while len(memo) > k:
memo[str[i]] -= 1
if memo[str[i]] == 0:
del memo[str[i]]
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
d = {}
for i in str:
if i not in d:
d[i] = 0
x = len(d)
ans = 999999
i = 0
j = 0
c = 0
while i < len(str):
if d[str[i]] == 0:
c += 1
d[str[i]] += 1
if c == x:
f = True
while c == x:
ans = min(ans, i - j + 1)
d[str[j]] -= 1
if d[str[j]] == 0:
c -= 1
j += 1
i += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, a):
s = ""
a1 = {}
for i in a:
a1[i] = 1
c1 = len(a1)
i = 0
j = 0
a2 = {}
c = 0
res = len(a)
while j < len(a):
if a[j] not in a2:
a2[a[j]] = 0
c += 1
a2[a[j]] += 1
while i <= j and c == c1:
res = min(res, j - i + 1)
a2[a[i]] -= 1
if a2[a[i]] == 0:
del a2[a[i]]
c -= 1
i += 1
j += 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
n = len(s)
d = {}
count = 0
for i in range(n):
d[s[i]] = 0
i = 0
j = 0
ans = n
while i < n:
if d[s[i]] == 0:
count += 1
d[s[i]] += 1
if count == len(d):
while j < n and d[s[j]] > 1:
d[s[j]] -= 1
j += 1
if ans > i - j + 1:
ans = i - j + 1
i += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
d = {}
for i in str:
if i not in d:
d[i] = 0
i, j = 0, float("inf")
count, out = 0, float("inf")
for j in range(len(str)):
if d[str[j]] == 0:
count += 1
d[str[j]] += 1
if count == len(d):
while i < j:
d[str[i]] -= 1
if d[str[i]] == 0:
out = min(out, j - i + 1)
count -= 1
i += 1
break
i += 1
return out if out != float("inf") else 1
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
dict = {}
for i in str:
if i in dict:
dict[i] += 1
else:
dict[i] = 1
count = len(list(dict.keys()))
i = j = 0
ans = len(str)
c = 0
dict = {}
for i in range(len(str)):
if str[i] in dict:
dict[str[i]] += 1
else:
dict[str[i]] = 1
c += 1
if c == count:
ans = min(ans, i - j + 1)
while c == count and j <= i:
dict[str[j]] -= 1
if dict[str[j]] == 0:
del dict[str[j]]
c -= 1
ans = min(ans, i - j + 1)
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
reslen = len(str)
s = set()
d = dict()
for i in range(len(str)):
s.add(str[i])
i = 0
count = 0
for j in range(len(str)):
d[str[j]] = d.get(str[j], 0) + 1
if d[str[j]] == 1:
count += 1
if count == len(s):
while d[str[i]] > 1:
if d[str[i]] > 1:
d[str[i]] -= 1
i += 1
if reslen > j - i + 1:
reslen = j - i + 1
return reslen
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
n = len(s)
res = n
i = 0
uniq = set(list(s))
found = {}
for j in range(n):
if s[j] in found:
found[s[j]] += 1
else:
found[s[j]] = 1
while i < j:
if found[s[i]] > 1:
found[s[i]] -= 1
i += 1
else:
break
if len(found) == len(uniq):
res = min(res, j - i + 1)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
ans = len(str)
N = len(str)
n = len(set(str))
i, j = 0, 0
d = {}
while i < N:
if str[i] not in d:
d[str[i]] = 1
else:
d[str[i]] += 1
if len(d) == n:
while d[str[j]] > 1:
d[str[j]] -= 1
j += 1
ans = min(ans, i - j + 1)
i += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
d = {}
for i in str:
d[i] = 0
i = 0
j = 0
ans = len(str)
count = len(d)
temp = 0
while j < len(str):
while temp < count and j < len(str):
if d[str[j]] == 0:
temp += 1
d[str[j]] += 1
j += 1
while temp >= count:
d[str[i]] -= 1
if d[str[i]] == 0:
temp -= 1
i += 1
ans = min(ans, j - i + 1)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
l = len(str)
s = set()
for i in range(len(str)):
s.add(str[i])
n = len(s)
head = 0
tail = 0
hmap = {}
ans = l
while head < l:
if str[head] in hmap:
hmap[str[head]] += 1
else:
hmap[str[head]] = 1
if len(hmap) == n:
while hmap[str[tail]] > 1:
hmap[str[tail]] -= 1
tail += 1
ans = min(ans, head - tail + 1)
head += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
p = len(set(str))
j = 0
i = 0
d = {}
mn = 100000
while j < len(str):
if str[j] in d:
d[str[j]] += 1
else:
d[str[j]] = 1
if len(d) == p:
while len(d) == p:
mn = min(mn, j - i + 1)
d[str[i]] -= 1
if d[str[i]] == 0:
del d[str[i]]
i += 1
j += 1
return mn
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, input_string):
start = 0
end = 1
alphabet_dict = {}
distinct_list = list(set(input_string))
for i in range(0, len(distinct_list)):
alphabet_dict[distinct_list[i]] = 0
n = len(distinct_list)
count = 1
alphabet_dict[input_string[0]] = 1
answer = len(input_string)
while start <= end < len(input_string):
if count < n:
element = input_string[end]
if alphabet_dict[element] == 0:
alphabet_dict[element] = 1
count = count + 1
else:
alphabet_dict[element] = alphabet_dict[element] + 1
end = end + 1
elif count == n:
answer = min(answer, end - start)
element = input_string[start]
if element in alphabet_dict and alphabet_dict[element] == 1:
count = count - 1
alphabet_dict[element] = alphabet_dict[element] - 1
start = start + 1
while count == n:
answer = min(answer, end - start)
element = input_string[start]
if element in alphabet_dict and alphabet_dict[element] == 1:
count = count - 1
alphabet_dict[element] = alphabet_dict[element] - 1
start = start + 1
return answer
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
d = {}
for ch in str:
if ch not in d:
d[ch] = 1
n = len(d)
d.clear()
i = 0
j = 0
count = 0
mini = len(str)
while j < len(str):
if str[j] not in d:
d[str[j]] = 1
count = count + 1
else:
d[str[j]] = d[str[j]] + 1
if count == n:
while d[str[i]] != 1:
d[str[i]] = d[str[i]] - 1
i = i + 1
mini = min(mini, j - i + 1)
j = j + 1
return mini
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
freq = {}
for c in s:
freq[c] = 0
unique_chars = len(freq)
left = 0
right = 0
count = 0
min_length = float("inf")
while right < len(s):
if s[right] in freq:
freq[s[right]] += 1
if freq[s[right]] == 1:
count += 1
right += 1
while count == unique_chars:
if right - left < min_length:
min_length = right - left
if s[left] in freq:
freq[s[left]] -= 1
if freq[s[left]] == 0:
count -= 1
left += 1
return min_length
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
distinct = len(set(s))
d = dict()
si = -1
Len = 100000.0
start = 0
for i in range(len(s)):
if s[i] not in d:
d[s[i]] = 1
else:
d[s[i]] += 1
if len(d) == distinct:
while d[s[start]] > 1:
d[s[start]] -= 1
start += 1
clen = i - start + 1
if Len > clen:
Len = clen
si = start
return len(s[si : si + Len])
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
dict = {}
ans = 1000000000.0
for i in str:
if i not in dict:
dict[i] = 1
dict2 = {}
j = 0
for i in range(len(str)):
if str[i] not in dict2:
dict2[str[i]] = 1
else:
dict2[str[i]] += 1
while len(dict) == len(dict2):
ans = min(ans, i - j + 1)
if dict2[str[j]] > 1:
dict2[str[j]] -= 1
elif dict2[str[j]] == 1:
dict2.pop(str[j])
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
mp = {}
cnt = 0
for i in range(len(str)):
if str[i] not in mp:
mp[str[i]] = 0
cnt += 1
cnt1 = 0
j = 0
mn = len(str)
for i in range(len(str)):
if mp[str[i]] == 0:
mp[str[i]] += 1
cnt1 += 1
else:
mp[str[i]] += 1
while cnt == cnt1:
mn = min(mn, i - j + 1)
if mp[str[j]] == 1:
mp[str[j]] -= 1
cnt1 -= 1
j = j + 1
else:
mp[str[j]] -= 1
j = j + 1
return mn
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
D = {}
for i in s:
if i in D:
pass
else:
D[i] = 1
n = len(s)
i, j = 0, 0
count = len(D)
mini = 9999
while j < n:
if s[j] in D:
D[s[j]] -= 1
if D[s[j]] == 0:
count -= 1
while count == 0:
mini = min(mini, j - i + 1)
if s[i] in D:
D[s[i]] += 1
if D[s[i]] > 0:
count += 1
i += 1
j += 1
return mini
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
nd = len(set(str))
i = 0
j = 0
res = len(str)
dic = {}
while i < len(str):
if str[i] in dic:
dic[str[i]] = dic[str[i]] + 1
else:
dic[str[i]] = 1
if len(dic) == nd:
while dic[str[j]] > 1:
dic[str[j]] = dic[str[j]] - 1
j = j + 1
res = min(res, i - j + 1)
i = i + 1
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, a):
dict = {}
n = len(set(a))
left = 0
right = 0
ans = len(a)
while right < len(a):
if a[right] not in dict:
dict[a[right]] = 1
else:
dict[a[right]] += 1
if len(dict) == n:
while dict[a[left]] > 1:
dict[a[left]] -= 1
left += 1
ans = min(ans, right - left + 1)
right += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, s):
n = len(s)
distinct_chars = len(set(s))
freq = [0] * 256
left = 0
right = 0
count = 0
min_len = n
while right < n:
ch = ord(s[right])
if freq[ch] == 0:
count += 1
freq[ch] += 1
right += 1
while count == distinct_chars:
min_len = min(min_len, right - left)
ch = ord(s[left])
freq[ch] -= 1
if freq[ch] == 0:
count -= 1
left += 1
return min_len
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
n = len(str)
dic, vic = {}, {}
for a in str:
if a not in dic:
dic[a] = 0
dic[a] += 1
i, j, ans = 0, 0, 10000000000
while j < n:
if str[j] not in vic:
vic[str[j]] = 0
vic[str[j]] += 1
if len(vic) == len(dic):
while len(vic) == len(dic):
vic[str[i]] -= 1
if vic[str[i]] == 0:
del vic[str[i]]
i += 1
ans = min(ans, 2 + j - i)
j += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
res = float("inf")
i, j = 0, 0
maxLen = len(set(list(str)))
hashmap = {}
while j < len(str):
if str[j] not in hashmap:
hashmap[str[j]] = 1
else:
hashmap[str[j]] += 1
j += 1
if len(hashmap) == maxLen:
while i < j and hashmap[str[i]] > 1:
hashmap[str[i]] -= 1
i += 1
res = min(res, j - i)
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, S):
distinct_chars = set(S)
n = len(S)
left = 0
min_length = n
count = [0] * 256
distinct = 0
for right in range(n):
count[ord(S[right])] += 1
if count[ord(S[right])] == 1:
distinct += 1
if distinct == len(distinct_chars):
while count[ord(S[left])] > 1:
count[ord(S[left])] -= 1
left += 1
min_length = min(min_length, right - left + 1)
count[ord(S[left])] -= 1
left += 1
distinct -= 1
return min_length
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given a string 's'. The task is to find the smallest window length that contains all the characters of the given string at least one time.
For eg. A = aabcbcdbca, then the result would be 4 as of the smallest window will be dbca.
Example 1:
Input : "AABBBCBBAC"
Output : 3
Explanation : Sub-string -> "BAC"
Example 2:
Input : "aaab"
Output : 2
Explanation : Sub-string -> "ab"
Example 3:
Input : "GEEKSGEEKSFOR"
Output : 8
Explanation : Sub-string -> "GEEKSFOR"
Your Task:
You don't need to read input or print anything. Your task is to complete the function findSubString() which takes the string S as input and returns the length of the smallest such window of the string.
Expected Time Complexity: O(256.N)
Expected Auxiliary Space: O(256)
Constraints:
1 ≤ |S| ≤ 10^{5}
String may contain both type of English Alphabets.
|
class Solution:
def findSubString(self, str):
s = set(str)
n = len(s)
ss = set()
ind = 0
d = {}
mini = 10**9
for i in range(len(str)):
if str[i] not in ss:
ss.add(str[i])
d[str[i]] = d.get(str[i], 0) + 1
if len(ss) == n:
ind = i + 1
mini = min(mini, i + 1)
break
index = 0
while d[str[index]] > 1:
d[str[index]] -= 1
index += 1
mini = min(mini, i - index + 1)
for i in range(ind, len(str)):
d[str[i]] = d.get(str[i], 0) + 1
while d[str[index]] > 1:
d[str[index]] -= 1
index += 1
mini = min(mini, i - index + 1)
while d[str[index]] > 1:
d[str[index]] -= 1
index += 1
mini = min(mini, i - index + 1)
return mini
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
def newLL(head, tail):
l1 = Linked_List()
while head != tail.next:
l1.insert(head.data)
head = head.next
return l1.head
anagrams = list()
s = sorted(s)
tail = head
k = len(s) - 1
while tail and k:
tail = tail.next
k -= 1
copy = head
while tail:
wind = list()
while copy != tail.next:
wind.append(copy.data)
copy = copy.next
if sorted(wind) == s:
anagrams.append(newLL(head, tail))
head = tail.next
k = len(s)
while tail and k:
tail = tail.next
k -= 1
else:
tail = tail.next
head = head.next
copy = head
return anagrams
|
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def anagram(self, smp, cmp):
if len(smp) != len(cmp):
return False
for k, v in smp.items():
if cmp.get(k) != v:
return False
return True
def findAnagrams(self, head, s):
ans = []
prev = None
smp = {}
cmp = {}
curr = head
n = len(s)
for i in range(n):
if s[i] in smp:
smp[s[i]] += 1
else:
smp[s[i]] = 1
while curr != None:
temp = curr
for i in range(n):
if temp == None:
break
if temp.data in cmp:
cmp[temp.data] += 1
else:
cmp[temp.data] = 1
prev = temp
temp = temp.next
if self.anagram(smp, cmp):
prev.next = None
ans.append(curr)
curr = temp
else:
curr = curr.next
cmp.clear()
return ans
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NONE IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
def check1(counts, window):
for i in range(97, 123):
cur = chr(i)
if window.get(cur, 0) < counts.get(cur, 0):
return False
return True
def check2(counts, window):
for i in range(97, 123):
cur = chr(i)
if window[cur] != counts[cur]:
return False
return True
res = []
counts = {chr(i): (0) for i in range(97, 123)}
for i in s:
if i in s:
counts[i] += 1
window = {chr(i): (0) for i in range(97, 123)}
prev = None
ptr = head
n = len(s)
for i in range(n):
if s[i] in window:
window[ptr.data] += 1
prev = ptr
ptr = ptr.next
beg = head
while beg and ptr:
if check2(counts, window):
prev.next = None
res.append(beg)
x = beg
window = {chr(i): (0) for i in range(97, 123)}
beg = ptr
for i in range(n):
if not ptr:
for i in res:
ptr = i
for i in range(n - 1):
ptr = ptr.next
ptr.next = None
return res if res else -1
window[ptr.data] += 1
ptr = ptr.next
continue
window[ptr.data] += 1
window[beg.data] -= 1
beg = beg.next
prev = ptr
ptr = ptr.next
if check2(counts, window):
res.append(beg)
prev.next = None
for i in res:
ptr = i
for i in range(n - 1):
ptr = ptr.next
ptr.next = None
return res
|
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
slide = [0] * 26
for i in s:
slide[ord(i) - 97] += 1
make = [0] * 26
n = len(s)
i = 0
ans = []
h = head
prev = head
while h:
if i < n:
make[ord(h.data) - 97] += 1
elif slide == make:
prev.next = None
ans.append(head)
head = h
i = 0
make = [0] * 26
make[ord(h.data) - 97] += 1
else:
make[ord(head.data) - 97] -= 1
make[ord(h.data) - 97] += 1
head = head.next
prev = h
h = h.next
i += 1
if slide == make:
prev.next = None
ans.append(head)
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
ans = []
start, end = head, head
n = len(s)
mp1 = {}
for c in s:
mp1[c] = mp1.get(c, 0) + 1
count1 = len(mp1)
mp2 = {}
count2 = 0
while end:
mp2[end.data] = mp2.get(end.data, 0) + 1
if mp1.get(end.data, 0) == mp2[end.data]:
count2 += 1
while mp2[end.data] > mp1.get(end.data, 0):
if mp2[start.data] == mp1.get(start.data, 0):
count2 -= 1
mp2[start.data] -= 1
start = start.next
if count2 == count1:
ans.append(start)
ahead = end.next
end.next = None
start, end = ahead, ahead
mp2.clear()
count2 = 0
else:
end = end.next
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def checkAnagram(self, A, B):
for i in range(26):
if A[i] != B[i]:
return False
return True
def checkAndPush(self, isAnagram, n, ptr1, ptr2, res, freq1):
if isAnagram == False:
return
prev = None
res.append(ptr1)
temp = ptr2
ptr2 = ptr2.next
if temp != None:
temp.next = None
ptr1 = ptr2
for i in range(26):
freq1[i] = 0
for i in range(n):
if ptr2 != None:
freq1[ord(ptr2.data) - ord("a")] += 1
prev = ptr2
if ptr2 != None:
ptr2 = ptr2.next
ptr2 = prev
def findAnagrams(self, head, s):
n = len(s)
freq = [0] * 26
for ch in s:
freq[ord(ch) - ord("a")] += 1
freq1 = [0] * 26
ptr1 = head
ptr2 = head
prev = None
for i in range(n):
if ptr2 != None:
freq1[ord(ptr2.data) - ord("a")] += 1
prev = ptr2
ptr2 = ptr2.next
ptr2 = prev
res = []
while ptr2.next != None:
ok = self.checkAnagram(freq1, freq)
if ok:
if ok == False:
return
prev = None
res.append(ptr1)
temp = ptr2
ptr2 = ptr2.next
if temp != None:
temp.next = None
ptr1 = ptr2
for i in range(26):
freq1[i] = 0
for i in range(n):
if ptr2 != None:
freq1[ord(ptr2.data) - ord("a")] += 1
prev = ptr2
if ptr2 != None:
ptr2 = ptr2.next
ptr2 = prev
else:
freq1[ord(ptr1.data) - ord("a")] -= 1
ptr1 = ptr1.next
ptr2 = ptr2.next
freq1[ord(ptr2.data) - ord("a")] += 1
ok = self.checkAnagram(freq1, freq)
if ok:
res.append(ptr1)
return res
|
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR NUMBER RETURN ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
N = len(s)
TAR = [0] * 26
for c in s:
TAR[ord(c) - 97] += 1
ans, h, t, F = [], head, head, None
while t:
if F is None:
F = [0] * 26
for _ in range(N):
if t is None:
break
F[ord(t.data) - 97] += 1
t = t.next
else:
F[ord(h.data) - 97] -= 1
F[ord(t.data) - 97] += 1
h, t = h.next, t.next
if F == TAR:
ans.append(h)
while h.next != t:
h = h.next
h.next = None
h = t
F = None
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR LIST VAR VAR NONE WHILE VAR IF VAR NONE ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NONE VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
Dout = dict()
for b in s:
Dout[b] = Dout.get(b, 0) + 1
adv = head
out = []
Din = dict()
while adv and adv.next:
start = adv
i = 0
while i < len(s) and adv:
Din[adv.data] = Din.get(adv.data, 0) + 1
tail = adv
adv = adv.next
i += 1
if Din == Dout:
tail.next = None
out.append(start)
Din = dict()
else:
adv = start.next
Din = dict()
if out:
return out
a = Node()
a.data = "-1"
return [a]
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING RETURN LIST VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
n = len(s)
arr = [0] * 26
for i in s:
arr[ord(i) - 97] += 1
run = [0] * 26
ans = []
h = head
t = head
cnt = 0
while h and t:
ind = ord(t.data) - ord("a")
run[ind] += 1
cnt += 1
if cnt > n:
ind = ord(h.data) - ord("a")
run[ind] -= 1
cnt -= 1
h.next, h = None, h.next
if run == arr:
t.next, t = None, t.next
ans.append(h)
run = [0] * 26
cnt = 0
h = t
else:
t = t.next
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NONE VAR IF VAR VAR ASSIGN VAR VAR NONE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
str_list = [0] * 26
size = len(s)
for it in s:
str_list[ord(it) - ord("a")] += 1
ll = [0] * 26
res = []
count = 0
i = head
j = head
while j is not None:
count += 1
ll[ord(j.data) - ord("a")] += 1
if count == size:
if ll == str_list:
t = j.next
j.next = None
res.append(i)
j = t
i = j
count = 0
ll = [0] * 26
else:
ll[ord(i.data) - ord("a")] -= 1
i = i.next
j = j.next
count -= 1
else:
j = j.next
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
d = {}
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
st = head
en = head
ans_list = []
count = len(s)
flag = False
dcount = len(d)
while st != None:
if flag == False:
if st.data in d:
d[st.data] -= 1
if d[st.data] == 0:
dcount -= 1
if dcount == 0:
linked = Linked_List()
i = en
while i != st:
linked.insert(i.data)
i = i.next
linked.insert(i.data)
ans_list.append(linked.head)
flag = True
st = st.next
if count <= 1:
if en.data in d:
if d[en.data] == 0:
dcount += 1
d[en.data] += 1
en = en.next
if en == st:
flag = False
count = len(s) + 1
count -= 1
return ans_list
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NONE IF VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
d = {}
for x in s:
d[x] = d.get(x, 0) + 1
cd = d.copy()
output = []
currhead = head
while currhead != None:
curr = currhead
count = 0
while curr != None:
if cd.get(curr.data, 0) == 0:
currhead = currhead.next
cd = d.copy()
count = 0
break
else:
cd[curr.data] = cd[curr.data] - 1
count += 1
if count == len(s):
count = 0
cd = d.copy()
output.append(currhead)
currhead = curr.next
curr.next = None
break
curr = curr.next
return output
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
s = sorted(s)
n = len(s)
ans = []
right_p = head
curr_count = 0
while right_p:
if curr_count == 0:
curr = []
if right_p.data in s:
dummy = right_p
curr.append(right_p.data)
curr_count += 1
right_p = right_p.next
elif curr_count < n - 1:
if right_p.data in s:
curr.append(right_p.data)
curr_count += 1
right_p = right_p.next
else:
right_p = dummy.next
curr_count = 0
else:
curr.append(right_p.data)
if sorted("".join(curr)) == s:
temp = right_p.next
right_p.next = None
ans.append(dummy)
right_p = temp
else:
right_p = dummy.next
curr_count = 0
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR
|
Given a linked list of characters and a string S.Return all the anagrams of the string present in the given linked list.In case of overlapping anagrams choose the first anagram from left.
Example 1:
Input: a -> b -> c -> a -> d -> b -> c -> a
S = bac
Output: [a -> b -> c, b -> c -> a]
Explanation: In the given linked list,
there are three anagrams:
1. a -> b -> c -> a -> d -> b -> c -> a
2. a -> b -> c -> a -> d -> b -> c -> a
3. a -> b -> c -> a -> d -> b -> c -> a
But in 1 and 2, a -> b -> c and b -> c-> a
are ovelapping.So we take a -> b -> c as it
comes first from left.So the output is:
[a->b->c,b->c->a]
Example 2:
Input: a -> b -> d -> c -> a
S = bac
Output: -1
Explanation: There is no anagrams, so output is -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function findAnagrams() which takes head node of the linked list and a string S as input parameters and returns an array of linked list which only stores starting point of the Anagram. If there is no anagram in the linked list, leave the Array empty.
Expected Time Complexity: O(N), where N is length of LinkedList
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1 <= |S| <= 10^{5}
|
class Solution:
def findAnagrams(self, head, s):
d = {}
for v in s:
if v in d:
d[v] += 1
else:
d[v] = 0
count = len(s)
temp = head
i = 0
l = []
while temp != None and temp.next != None:
temp1 = temp
d1 = {}
i = 0
while i < count and temp != None:
if temp.data in d1:
d1[temp.data] += 1
else:
d1[temp.data] = 0
temp2 = temp
temp = temp.next
i += 1
if d1 == d:
temp2.next = None
l.append(temp1)
else:
temp = temp1.next
if l:
return l
a = Node()
a.data = "-1"
l.append(a)
return l
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR RETURN VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.