HELP!!!Longest Substring with At Most K Distinct Characters. PYTHON 3

The example in the above question 'eceba' has the result 3 with K = 2, so shouldn't the answer for 'aba', k= 1 be 2. My answer is 2 but however it fails to pass the test case 'aba', k =1 for being 1. I dont understand the question properly. Can anyone explain
'''
class Solution:
def lengthOfLongestSubstringKDistinct(self, s: str, k: int) -> int:
length = 0
u = 0
n = len(s)
ans = []
for i in range(len(s)):
if k == 0:
return 0
if s[i] in ans:
ans.append(s[i])
length += 1
elif s[i] not in ans and u < k:
ans.append(s[i])
u += 1
length += 1
return length
'''

Comments (0)