my code fails for multiple test cases
my code
input where code fails
qwertyuioplkjhgfdsazxcvbn
Your Output is:
1
correct output is 25
Maximum length unique character substring sliding window problem
let’s run your code for the input you have provided, but with little variation:
- qwertyuioplkjhgfdsazxcvbn
initially, max_length is 1.
output is 1
expected output is 25
Now, adding an additional character q in the same string at different places:
2. qwertyuioplkjhgfdsazxcvbnq
initially, max_length is 1.
when second q encountered, max_length is updated to 25.
output is 25
expected output is 25
-
qwertyuioplkjhgfdsazxcvqbn
initially, max_length is 1.
when second q encountered, max_length is updated to 23.
output is 23
expected output is 23 -
qweqrtyuioplkjhgfdsazxcvbn
initially, max_length is 1.
when second q encountered, max_length is updated to 3.
output is 3
expected output is 23
Have, you noticed anything? Sure.
if you have found your mistake, then excellent.
if you haven’t, don’t worry i am here.
mistake:
you are updating the max_length only if you find another occurrence of any character. if you see the second example, the value of max_length got updated when the second occurrence of q is encountered.
Now, observe the first example, as all the characters are uniques, so the max_length was not updated.
Observe the other two examples yourself.
Solution:
add the following statement after the loop, so ur program would check for length after your entire string is traversed.
max_len=max(max_len,curr_len);
I hope this would help.
if you still have doubts, feel free to ask.
if your doubt is clear, mark it as resolved and if possible, give a like and reviews about me.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.