educative.io

Why am I getting indexout of Bound exception here?

def longest_substring_with_k_distinct(str1, k):
start = 0
maxLength = 0
freqMap = {}

for end in range(len(str1)):
right = str1[end]
if right not in freqMap:
freqMap[right] = 0
freqMap[right] +=1

while len(freqMap) > k:
  left = str1[start]
  freqMap[left] =-1
  if freqMap[left] == 0:
    del freqMap[left]
  start +=1

maxLength = max(maxLength,end - start + 1)
return maxLength

Output Exception:

Traceback (most recent call last):
File “main.py”, line 82, in
main()
File “main.py”, line 62, in main
results = executeTests()
File “main.py”, line 47, in executeTests
actual_output = longest_substring_with_k_distinct(inputArrays[i], inputs[i])
File “main.py”, line 13, in longest_substring_with_k_distinct
left = str1[start]
IndexError: string index out of range

can you provide the test case its failing on? also could you provide the code indented properly