educative.io

Getting max_len value len(window_size) +1

Hello There ,
I wanted to understand with my below code , why am I getting max_len value len(window_size) +1, always.

def longest_substring_with_k_distinct(str1, k):
window_size=’’
max_len=0
window_start = 0
for window_end in range(len(str1)):
window_size += str1[window_end]
if (len(set(window_size)) > k):
max_len =max(max_len,len(window_size))
window_size = window_size[1:0]
return max_len

I got mistake, fixed it by

def longest_substring_with_k_distinct(str1, k):
window_size=’’
max_len=0
window_start = 0
for window_end in range(len(str1)):
window_size += str1[window_end]
while (len(set(window_size)) > k):
max_len =max(max_len,len(window_size) -1 )
window_size = window_size[1:0]
return max_len